Abstract classes and Abstract methods in Java with Examples rcub.
Abstract class :
A class which is declared with the abstract keyword is known as an abstract class. The partially implemented class is called as abstract class. It is used for abstraction. we cannot create object for abstract class.
Syntax :
public abstract class <classname>
{
<abstract methods>;
<non-abstract methods>;
}
Example :
abstract class A
{
abstract void area();
A()
{
System.out.println("welcome");
}
static void show()
{
System.out.println("static method");
}
}
Abstract Method :
A method which is declared as abstract and does not have implementation is known as an abstract method. The method contains only declaration.
Syntax :
abstract type name(parameter-list);
Example :
abstract void area();
//no method body and abstract modifier
Rules for using abstract modifier:
1.An abstract class must be declared with an abstract keyword and an abstract method must be declared with an abstract keyword.
2. abstract class can have abstract and non-abstract methods.
3.It cannot be instantiated.(we cannot create object of abstract class ).
4. Class can have constructors and static methods also. But you cannot declare abstract constructors, or abstract static methods or final abstract methods. constructor of abstract class is called when an object of a child class is created.
5. If a class has at least one abstract method, then the class must be declared abstract.
6.If a child does not implement all the abstract methods of abstract parent class, then the child class must need to be declared abstract as well.
7.Abstract classes can also have final methods (methods that cannot be overridden).
8.abstract method contains a method signature, but no method body.
A class which is not abstract is referred as Concrete class.
1.Program to implement abstract classes and abstract methods in Java.
abstract class A
{
abstract void call();
A()
{
System.out.println("abstract modifier demo") ;
}
public void display( )
{
System.out.println("this is non-abstract method");
}
}
class B extends A
{
void call()
{
System.out.println("this is abstract method implementation");
}
public static void main(String args[] )
{
B b = new B();
b.display();
b.call();
}
}
OUTPUT:
abstract modifier demo
this is non abstract method
this is abstract method implementation.
2.Program to demonstrate usage of abstract classes and abstract methods in Java.
abstract class shape
{
abstract double area();
}
class rectangle extends shape
{
double l, b;
{
l=2.0; b=4.0;
}
double area()
{
return l*b;
}
}
class circle extends shape
{
double r;
{
r=10.0;
}
double area()
{
return 3.142*r*r;
}
}
class square extends shape
{
double s;
{
s=3.0;
}
double area()
{
return s*s;
}
}
class compute
{
public static void main(String args[ ])
{
shape s;
rectangle r=new rectangle();
circle c=new circle();
square s1=new square() ;
System.out.println("calculate area of Geometrical figures");
s=r;
System.out.println("area of rectangle="+s. area()) ;
s=s1;
System.out.println("area of square="+
s.area()) ;
s=c;
System.out.println("area of circle="+s.area()) ;
}
}
Output:
calculate area of Geometrical figures
area of rectangle=8.0
area of square=9.0
area of circle=314.2
here, it is not possible to create an object of type shape, you can create a reference
variable of type Shape. The variable s is declared as a reference to shape.
it follows the concept of dynamic method dispatch. Upcasting means parent class reference variable holds the object of it's child class.
Upcasting :
s=r;
s=c;
s=s1;
where s is an reference variable of parent class shape.
r, c & s1 are the objects of child class rectangle, circle & square respectively.
Why can’t we create the object of an abstract class?
Because these classes are incomplete, or partially implemented.
they have abstract methods that have no body . But we can create reference variable for abstract class.
Comments
Post a Comment