Abstract class in Java.

Abstract class in Java

1.Abstract class is an important element in java which is declared with abstract keyword.
2.It is a collection of abstract method/function or non-abstract method.
3.We can not create instance(object) of an abstract class.
4.We can not define abstract function inside abstract class only can be declared,so it is the responsibility of derived class to implement the method/function of abstract class.
5.An abstract class is also extended by a class.

Example 1 :Abstract class withoud abstract method

abstract class Geometry
{
 //declaring non-abstract method
 public void rectangle_area(int height,int width)
 {
  int ar=height*width;
  System.out.println("Area of rectangle="+ar);
 }
  //declaring non-abstract method
 public void square_area(int side)
 {
  int ar=side*side;
  System.out.println("Area of square="+ar);
 }
  //declaring non-abstract method
 public void circle_area(float radius)
 {
  float ar=3.14f*radius*radius;
  System.out.println("Area of circle="+ar);
 }
          
}
//extending abstract class
class Easy extends Geometry
{
 public static void main(String[] args)
 {
   //creating instance of derived class
   Easy obj=new Easy();
   //calling method of abstract class
   obj.rectangle_area(12, 13);
   obj.square_area(12);
   obj.circle_area(2.2f);
 }
}
/*
### Output ###
Area of rectangle=156
Area of square=144
Area of circle=15.197601
*/

Abstract method/function in Java

1.abstract function is declared with abstract keyword.
2.It can be declared only inside abstract class.
3.It can not be defined inside abstract class ,only can be declared so it is the responsibility of derived class to implement abstract method.

Example 1 :Abstract class with abstract method

abstract class Geometry
{
//declaring abstract method
  abstract void rectangle_area(int height,int width);
  abstract void square_area(int side);  
  abstract void circle_area(float radius);          
}
//extending abstract class
class Easy extends Geometry
{
 //implementing abstrcat method of abstract class
 public void rectangle_area(int height,int width)
 {
  int ar=height*width;
  System.out.println("Area of rectangle="+ar);
 }
//implementing abstrcat method of abstract class
 public void square_area(int side)
 {
  int ar=side*side;
  System.out.println("Area of square="+ar);
 }
 //implementing abstrcat method of abstract class
 public void circle_area(float radius)
 {
  float ar=3.14f*radius*radius;
  System.out.println("Area of circle="+ar);
 }
 public static void main(String[] args)
 {
   //creating instance of derived class
   Easy obj=new Easy();
   //calling abstract method
   obj.rectangle_area(12, 13);
   obj.square_area(12);
   obj.circle_area(2.2f);
 }
}
/*
### Output ###
Area of rectangle=156
Area of square=144
Area of circle=15.197601
*/

Example 2 :Abstract class with abstract and non-abstract method

abstract class Geometry
{
  //declaring abstract method
  abstract void rectangle_area(int height,int width);
   //declaring non-abstract or normal method
 public void square_area(int side)
 {
  int ar=side*side;
  System.out.println("Area of square="+ar);
 }
  //declaring non-abstract or normal method
 public void circle_area(float radius)
 {
  float ar=3.14f*radius*radius;
  System.out.println("Area of circle="+ar);
 }
}
//extending abstract class
class Easy extends Geometry
{
 //implementing abstrcat method of abstract class
 public void rectangle_area(int height,int width)
 {
  int ar=height*width;
  System.out.println("Area of rectangle="+ar);
 }
 public static void main(String[] args)
 {
   //creating instance of derived class
   Easy obj=new Easy();
   //calling abstract method
   obj.rectangle_area(12, 13);
   //calling non-abstract or normal method
   obj.square_area(12);
   obj.circle_area(2.2f);
 }
}
/*
### Output ###
Area of rectangle=156
Area of square=144
Area of circle=15.197601
*/
Next Post Previous Post
No Comment
Add Comment
comment url