• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

1.Whthout abstract Program

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");
}
}
class ab1
{
public static void main(String arhs[])
{
B b=new B();
b.show();
C c=new C();
c.show();
}
}


Output:
welcome
java
Note : In the above example parent class method body replaced by child class method body, to overcome this problem Abstract introduced

Abstract


Abstract class and Abstract method
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.
Sysntax:

abstract returntype methodname(list of parameters/no parameters);


Note:
If any class containing atleast one abstract method that class becomes abstract class.
Syntax:

abstract class classname
{
abstract returntype methodname(list of parameters/no parameters);
..
..
returntype methodname(list of parameters/no parameters)
{
..
..
}
..
..
}


Note: 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.

Key Points

Image
2.With abstract Program

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");
}
}
class ab1
{
public static void main(String arhs[])
{
B b=new B();
b.show();
C c=new C();
c.show();
}
}


Output:
welcome
java
3.Creating object for abstract class

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");
}
}
class ab1
{
public static void main(String arhs[])
{
A a=new A();
}
}


Output:
Main.java:23: error: A is abstract; cannot be instantiated
A a=new A();
Note 1:
We can not create object for the abstract class directly.
Note 2:
If you want to create a abstract object for the abstract class with the help of Derived class name
Syntax:

Parentclassname objectref=new Childclassname();
4.Creating object for abstract class

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");
}
}
class ab1
{
public static void main(String arhs[])
{
A a=new B();
}
}


Output:
5.Creating object for abstract class

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");
}
}
class ab1
{
public static void main(String arhs[])
{
A a=new B();
a.show();
a.disp();
}
}


Output:
welcome
This is abstract disp Method
Note:By using A class object we can call parent class methods(show,disp)
6.Creating object for abstract class

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");
}
}
class ab1
{
public static void main(String arhs[])
{
A a=new B();
a.show();
a.disp();
a.add();
}
}


Output:
Main.java:30: error: cannot find symbol
a.add();
}
^ symbol: method add()
location: variable a of type A
1 error

Note:
We can't call child class own methods by using abstract class object
7.Creating object for abstract class

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");
}
}
class ab1
{
public static void main(String arhs[])
{
B b=new B();
b.show();
b.disp();
b.add();
}
}


Output:
welcome
This is abstract disp Method
This is B class Add Method

Note:
By using child class object we can call abstract class methods and normal class methods
8.Creating object for abstract class

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");
}
}
class ab1
{
public static void main(String arhs[])
{
A a=new B();
a.show();
a.disp();
B b=new B();
b.show();
b.disp();
b.add();
}
}


Output:
welcome
This is abstract disp Method
welcome
This is abstract disp Method
This is B class Add Method

Note:
In the above example object created multiple times so memory allocated multiple times. To overcome this program down casting introduced. down casting
9.Down Casting

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");
}
}
class ab1
{
public static void main(String arhs[])
{
A a=new B();
a.show();
a.disp();
B b=(B)a;
b.show();
b.disp();
b.add();
}
}


Output:
welcome
This is abstract disp Method
welcome
This is abstract disp Method
This is B class Add Method