Posts

Showing posts from February, 2024

Inheritence_in_interface

  package com.company ; interface SampleInterface { void meth1 (); void meth2 (); } interface ChildSampleInterface extends SampleInterface { /* we can extend from an interface for inheritence to a interface and we can't do with a implements and a class */ void meth3 (); void meth4 (); } class MySampleClass implements ChildSampleInterface { public void meth3 (){ System . out . println ( "Meth3" ); } public void meth4 (){ System . out . println ( "Meth4" ); } public void meth1 (){ System . out . println ( "Meth1" ); } public void meth2 (){ System . out . println ( "Meth2" ); } } public class inheritence_interface { public static void main ( String [] args ) { MySampleClass m = new MySampleClass (); m . meth1 (); m . meth2 (); m . meth3 (); m . meth4 (); } } o/p Meth1 Meth2 Meth3 Meth4

Interface

  package com.company ; interface Bicycle { int a = 45 ; //in interface we can give the properties in interfaces and that is final void applyBrake ( int decrement ); void speedUp ( int increment ); /*this interface will give the force to that class to implement all the method if we implement the bicycle so we should implement applyBrake and speedUP */ } interface HornBicycle { void blowhornkk (); void blowhorndd (); } class AvonCycle implements Bicycle , HornBicycle { //here we can use the two interfaces in same class where in abstract class cannot int b = 10 ; void blowHorn (){ System . out . println ( "pee pee" ); } public void applyBrake ( int decrement ){ System . out . println ( "applying brake " ); } public void speedUp ( int increment ){ System . out . println ( "applying speedUp" ); } public void blowhornkk (){ System . out . println ( "Kabh...

Abstract

  package com.company ; /*Abstract class ke help se aur dusri class banegi standard abstract class have no object for abstract class extend the other class should have extract or the override the abstract _ when an abstract class is sub classed ,the subclass usually implementations for all of the methods in parent class.If doesn't it must be declared abstract */ abstract class Parent { public Parent (){ System . out . println ( "Mai base2 ka constructor hoon " ); } public void sayhello (){ System . out . println ( "Hello" ); } abstract public void greet (); abstract public void greet2 (); } class child2 extends Parent { @Override public void greet (){ System . out . println ( "Good morning " ); } @Override public void greet2 (){ System . out . println ( "Good Evening " ); }} abstract class child3 extends Parent { public void th (){ ...

Number_guessing_game

  package com.company ; import java.util.Random ; import java.util.Scanner ; public class game { int no_of_guesses ; int computerinput ; int inputno ; public int getNo_of_guesses () { return no_of_guesses ; } public void setNo_of_guesses ( int no_of_guesses ) { this . no_of_guesses = no_of_guesses ; } game (){ Random s = new Random (); this . computerinput = s . nextInt ( 100 ); } public void takeuserinput (){ System . out . println ( "Enter the number " ); Scanner sc = new Scanner ( System . in ); inputno = sc . nextInt (); } boolean iscorrectnumber (){ no_of_guesses ++; if ( inputno == computerinput ){ System . out . format ( "Yes you have guessed it correct it was %d \n you guessed it in %d attempts " , computerinput , no_of_guesses ); return true ; } else if ( inputno < computerinput ){ System . out . println ( "Too low.....

Dynamic_method_dipatch

Image
  package com.company ; class phone { public void Showtime (){ System . out . println ( "12:14 pm Sunday " ); } public void on (){ System . out . println ( "Phone on..." ); } } class smartphone extends phone { public void on (){ System . out . println ( "SmartPhone on..." ); } public void greet (){ System . out . println ( "Welcome you ..." ); } } public class Dynamic_method_dispatch { public static void main ( String [] args ) { // phone p=new phone(); // p.Showtime(); // p.on(); // smartphone sp=new smartphone(); // sp.greet(); // sp.on(); phone p = new smartphone (); //Correct /*in this one p is the object of smartphone and will run only the overriding function only and rest it will run the phone method only That is the concept of Dynamic method dispatch */ // smartphone ph=mew phone(...

Overriding

  package com.company ; //overloading means one class have more than one methods have same name but different argument/parameter class A { public void met2 () { System . out . println ( "I am a method 2 of class A" ); //met2 o/p of classA } public int kaps (){ return 10 ; } } class B extends A { @Override //it means we have used override and easy to understand we have used override public void met2 () { /*method overriding means the same name of methods of child class implements & parent class can run with same name but give different output and implementation */ //met2 o/p of classB System . out . println ( "I am a method 2 of class B" ); } public void met3 (){ System . out . println ( "I am a method 3 of class B" ); } } public class Method_overriding { public static void main ( St...

Inheritence in Constructor

  package com.company ; class base { base (){ System . out . println ( "I am a constructor " ); } base ( int a ){ System . out . println ( "I am a over loaded constructor of base class with the value a given is " + a ); } } class Derived extends base { Derived (){ // super(0);//super is a keyword where the only argument constructor will run from the base class System . out . println ( "I am a derived class constructor " ); } Derived ( int x , int y ){ super ( x ); //It takes the value of x from the argument constructor of derived class System . out . println ( "I am a over loaded constructor of derived class with the value y given as " + y ); } } public class Inheritence { public static void main ( String [] args ) { // base b=new base(); Derived der = new Derived ( 10 , 12 ); //First it takes the constructor from the base then it to derived } ...

Inheritence

 Inheritence in Java  package com.company ; class base { //this is base class int x ; public int getX () { return x ; } public void setX ( int x ) { System . out . println ( "I am in base and setting X now " ); this . x = x ; } public void PrintMe (){ System . out . println ( "i am a constructor" ); } } class Derived extends base { //this is inherit the properties of base int y ; public int getY () { return y ; } public void setY ( int y ) { this . y = y ; } } public class Inheritence { public static void main ( String [] args ) { System . out . println ( "Derived class" ); Derived der = new Derived (); der . setX ( 5 ); System . out . println ( der . getX ()); //taking values from the base class der . setY ( 10 ); System . out . println ( der . getY ()); der . PrintMe (); System . out . println ( "Base class" )...