How to create the Object of Abstract class…?

Object of Abstract class

How to create the object of abstract class..?

We can create the Object of Abstract class by two different ways.

1)By inheritance

  • Access the resources of the .class file after establish the relation is the concept of inheritance in java.
  • Inheritance is the object-oriented concept which is used to create a relation between two .class files.
  • Relation mean one .class file parent class(base class,super class ,master class) and another .class file.
  • We can access the member of another .class like own resource in case of inheritance.

2)By anonymous class

It is an inner class without a name and for which only a single object is created.

Class without any name is known as anonymous class.

The name of the class given by the compiler is known as an anonymous class.

When we are giving the body of an object then the java compiler implicitly create the dot class file(.class) technically known as anonymous class.

How to create the object of abstract class by inheritance.

The complete class should satisfied two rules in Java.

Rule-1:- it should not contain any abstract method.

Rule-2:-All the abstract method of the super abstract class as to be designed within the subclass.

PROGRAM

abstract class Test

{

                                abstract void show();  //method without any body

                                abstract void fun1();   //method without any body

                                int x=90;

                void display()

                {

                                System.out.println("display()....");

                }

}

abstract class Demo extends Test

{

                void show()   //give the body in subclass

                {

                                System.out.println("show()....");

                }

}

class MyClass extends Demo

{

                void fun1()     //give the body in subclass

                {

                                System.out.println("fun1()....");

                }

                                public static void main(String[] args) 

                {

                                MyClass mc=new MyClass();

                                mc.show();

                                mc.fun1();

                                System.out.println(mc.x);

                                mc.display();

                }

}

O/P  show()....

fun1()....

90

display()....


 

How to create the object of abstract class by anonymous class:-

PROGRAM:-

 

abstract class Area

{

      abstract void Area();    //abstract method without body

}

class Test

{

        public static void main(String[] args) 

         {

             Area a1=new Area()

               {

                       void Area()      //give the body in subclass

                       {

                            System.out.println("logic to found the Area of cicle");

                       }

                };

                  Area a2=new Area()

                   {

                      void Area()

                           {

                               System.out.println("logic to found the Area of rect");

                           }

                     };

                       a1.Area();

                       a2.Area();

                }

}

O/P logic to found the Area of cicle

                logic to found the Area of rect
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community

Leave a Reply