Abstract class in java in Details Step by Step



OOPS concepts in Java

Java tutorials for beginners

Java basics concepts

Java Objects and Classes

What is Inheritance in Java?

What is Abstraction in Java?

What is Interface in Java?

What is Abstract Class in Java?

What is Polymorphism in Java?

What is Encapsulation in Java?

Java programming example

How Java Program Work?

Hello World in Java

Java basics for beginners



Abstract class:

Abstract class defines a class that contains the abstraction without providing a complete implementation of method. The abstract keyword is used for abstract class and abstract method.

Abstract classes are used if you want to create a super class that only defines the variables and methods and sub class will be accessed the variables and methods for implementation.

Rule of Abstract class.

        1.   Abstract classes may or may not contain abstract methods.
        2.   Must be declared abstract keyword if a class has at least one abstract method.
        3.     The class cannot be instantiated if abstract is declared.
        4.     Must be provided implementations of all the abstract methods, If you inherit            an abstract class.
         
         Example:

abstract class Figure {
    
      double dim1;
      double dim2;
    
      Figure(double d1,double d2){
            dim1 = d1;
            dim2 = d2;
      }
    
      abstract void area();

  }

   class Rectangle extends Figure{
    
      Rectangle(double d1,douple d2){
            super(d1,d2);
      }
      //Override area of rectangle
      void area(){
            System.out.println("area of rectangle is"+ dim1*dim2);
          
      }
   }

   class Demo{
      public static void main(String args[]){
            Figure f = new Rectangle(4,7);
            f.area();
   }


  Output:

      area of rectangle is 28


1 comment:

  1. Excellent Post. Are you looking for Laptops for your training? Click here Laptop Price to select your idle Laptop in budget.

    ReplyDelete