What is Abstract class and Anonymous class in java…?

Abstract and Anonymous class:-

Abstract class:-

If the class name is declared as an abstract keyword it is known as abstract class. An abstract class may or may not contain an abstract method. Abstract class and anonymous class almost equivalent to using a named inner class.

If the class is abstract it cannot be instantiated(we cannot create the object).

An abstract method is a method that is declared, but it doesn’t contain any implementation.

If any method of a class is abstract then the corresponding class should be abstract anonymous.

The abstract class you can be declared fields that are not static and final and defined public protected and private methods.

Abstract in JDK is Abstract-Map which is the part of the collection framework.

PROGRAM:-

abstract class Test
{
abstract void show();
abstract void fun1();
int x=90;
void display()
{
System.out.println("display()....");
}
}
abstract class Demo extends Test
{
void show()
{
System.out.println("show()....");
}
}
class MyClass extends Demo
{
void fun1()
{
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()....

Anonymous Class:-

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.

An anonymous class is defined and instantiated in a single succinct expression using the new operator

PROGRAM;-

class Test
{
void show()
{
System.out.println("java is simple");
}
public static void main(String[] args) 
{
anonymous an1=new anonymous();
anonymous an2=new anonymous();
{
void show()
{
System.out.println("hello");
}
};
anonymous an3=new anonymous();
{

};
an1.show();
an2.show();
an3.show();
}
}

Note:-

In the above program java compiler creates 3 .class files
i)Test.class
ii)Test$1.class
iii)Test$2.class
Generally anonymous class always final.

basicittopic

basicittopic

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

Leave a Reply