Abstract class in Java:-
Abstract class is flexible as compare to interface because we can define normal method and declare abstract method both.means it is combination of Interface Features and Class Features.
We will use abstract modifier and public access specifier in abstract method
abstract class Classname
{
data member;
void fun()
{
}
abstract access specifier returntype functionname();
}
Rules for abstract class:-
1 We can not create object of abstract class ,we should create reference of it .
2 abstract modifier should be used in Class-name and Method-name
Advantage of Abstract class:-
1) Security :- we can not access abstract class directly hence if we want to hide details of actual class then we can create abstract.
2) data contract :- all abstract method must be defined by child class .
abstract class Area
{
float a=23,b=2; //normal instance variable
void triangle()
{
System.out.println(a*b/2);
}
abstract void rect(); //abstract method can be default and public
}
class Areaimple extends Area
{
void rect()
{
System.out.println(a*b);
}
}
Calling
Area obj = new AreaImple();
obj.triangle();
obj.rect();
Abstract class is flexible as compare to interface because we can define normal method and declare abstract method both.means it is combination of Interface Features and Class Features.
We will use abstract modifier and public access specifier in abstract method
abstract class Classname
{
data member;
void fun()
{
}
abstract access specifier returntype functionname();
}
Rules for abstract class:-
1 We can not create object of abstract class ,we should create reference of it .
2 abstract modifier should be used in Class-name and Method-name
Advantage of Abstract class:-
1) Security :- we can not access abstract class directly hence if we want to hide details of actual class then we can create abstract.
2) data contract :- all abstract method must be defined by child class .
abstract class Area
{
float a=23,b=2; //normal instance variable
void triangle()
{
System.out.println(a*b/2);
}
abstract void rect(); //abstract method can be default and public
}
class Areaimple extends Area
{
void rect()
{
System.out.println(a*b);
}
}
Calling
Area obj = new AreaImple();
obj.triangle();
obj.rect();
POST Answer of Questions and ASK to Doubt