Opening Hours :7AM to 9PM
class A { void show() { System.out.println("Hi"); } } class B extends A { void show() { System.out.println("Welcome"); } } class C extends A { void show() { System.out.println("Java"); } } public class ab1 { public static void main(String args[]) { B b=new B(); b.show(); C c=new C(); c.show(); } }
Note : In the above example parent class method body replaced by child class method body, to overcome this problem Abstract introduced
If any method contains only signature without any body part or implementation is knows as abstract method or incomplete method.
If any method is preceded by abstract keyword is known as abstract method.
abstract returntype methodname(list of parameters/no parameters);
If any class containing atleast one abstract method that class becomes abstract class.
abstract class classnameNote:
{ abstract returntype methodname1(list of parameters/no parameters); abstract returntype methodname2(list of parameters/no parameters); .. .. returntype methodname3(list of parameters/no parameters) { Statement 1; Statement 2; } returntype methodname4(list of parameters/no parameters) { Statement 3; Statement 4; } .. .. }
Abstract class contains both abstract methods and concrete methods.
The main aim of the abstract method is to overcome the same method in multiple derived classes with different implementation.
abstract class A { abstract void show(); } class B extends A { void show() { System.out.println("welcome"); } } class C extends A { void show() { System.out.println("java"); } } public class ab2 { public static void main(String args[]) { B b=new B(); b.show(); C c=new C(); c.show(); } }
abstract class A { abstract void show(); } class B extends A { void show() { System.out.println("welcome"); } } class C extends A { void show() { System.out.println("java"); } } public class ab3 { public static void main(String args[]) { A a=new A(); } }
We can not create object for the abstract class directly.
If you want to create a abstract object for the abstract class with the help of Derived class name
abstract class A { abstract void show(); } class B extends A { void show() { System.out.println("welcome"); } } class C extends A { void show() { System.out.println("java"); } } publicclass ab4 { public static void main(String args[]) { A a=new B(); } }
abstract class A { abstract void show(); void disp() { System.out.println("This is abstract disp Method"); } } class B extends A { void show() { System.out.println("welcome"); } } class C extends A { void show() { System.out.println("java"); } } public class ab5 { public static void main(String args[]) { A a=new B(); a.show(); a.disp(); } }
Note:By using 'A' class object we can call parent class methods(show,disp)(disp and show methods are 'A' class own methods)
abstract class A { abstract void show(); void disp() { System.out.println("This is abstract disp Method"); } } class B extends A { void show() { System.out.println("welcome"); } void add() { System.out.println("This is B class Add Method"); } } class C extends A { void show() { System.out.println("java"); } } public class ab6 { public static void main(String args[]) { A a=new B(); a.show(); a.disp(); a.add(); } }
We can't call child class own methods by using abstract class object( 'add' method 'B' class own Methods)
abstract class A { abstract void show(); void disp() { System.out.println("This is abstract disp Method"); } } class B extends A { void show() { System.out.println("welcome"); } void add() { System.out.println("This is B class Add Method"); } } class C extends A { void show() { System.out.println("java"); } } public class ab7 { public static void main(String args[]) { B b=new B(); b.show(); b.disp(); b.add(); } }
By using child class object we can call abstract class methods and child class methods
abstract class A { abstract void show(); void disp() { System.out.println("This is abstract disp Method"); } } class B extends A { void show() { System.out.println("welcome"); } void add() { System.out.println("This is B class Add Method"); } } class C extends A { void show() { System.out.println("java"); } } public class ab8 { public static void main(String args[]) { A a=new B(); a.show(); a.disp(); B b=new B(); b.show(); b.disp(); b.add(); } }
In the above example object created multiple times so memory allocated multiple times. To overcome this program down casting introduced.
abstract class A { abstract void show(); void disp() { System.out.println("This is abstract disp Method"); } } class B extends A { void show() { System.out.println("welcome"); } void add() { System.out.println("This is B class Add Method"); } } class C extends A { void show() { System.out.println("java"); } } public class ab9 { public static void main(String args[]) { A a=new B(); a.show(); a.disp(); B b=(B)a; b.show(); b.disp(); b.add(); } }