• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

1.Without 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");
    }
}
public class ab1
{
    public static void main(String args[])
    {
        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 keyword

Abstract class and Abstract method

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 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; } .. .. }
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.

MSK TechnologiesTrainings

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");
    }
}
public class ab2
{
    public static void main(String args[])
    {
        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");
    }
}
public class ab3
{
    public static void main(String args[])
    {
        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");
    }
}
publicclass ab4
{
    public static void main(String args[])
    {
        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");
    }
}
public class ab5
{
    public static void main(String args[])
    {
        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)(disp and show methods are 'A' class own methods)

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");
    }
}
public class ab6
{
    public static void main(String args[])
    {
        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( 'add' method 'B' class own Methods)

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");
    }
}
public class ab7
{
    public static void main(String args[])
    {
        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 child 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");
    }
}
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();
    }
}
                              

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");
    }
}
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();
    
    }
}

   
                                

Output:
welcome
This is abstract disp Method

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