Abstract class in java

Abstract class in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.

Abstract class

If a class contain any abstract method then the class is declared as an abstract class. An abstract class is never instantiated.
Abstract class

 It is used to provide abstraction. Although it does not provide 100% abstraction because it can also have a concrete method.
Syntax :
abstract class class_name { }

Abstract method

The method that is declared without anybody within an abstract class is called an abstract method. The method body will be defined by its subclass. An abstract method can never be final and static. Any class that extends an abstract class must implement all the abstract methods declared by the superclass.
Syntax :
abstract return_type function_name ();    // No definition

Example of Abstract class

abstract class A
{
 abstract void callme(); 
}
class B extends A
{
 void callme()
 {
  System.out.println("this is callme."); 
 }
 public static void main(String[] args)
 {
  B b = new B();
  b.callme();
 }
}
Output :
this is callme.

Abstract class with concrete(normal) method.

Abstract classes can also have normal methods with definitions, along with abstract methods.
abstract class A
{
 abstract void callme();
 public void normal()
 {
  System.out.println("this is concrete method");
 }  
}
class B extends A
{
 void callme()
 {
  System.out.println("this is callme."); 
 }
 public static void main(String[] args)
 {
  B b = new B();
  b.callme();
  b.normal();
 }
}
Output :
this is callme.
this is concrete method.

Points to Remember

  1. Abstract classes are not Interfaces. They are different, we will study this when we will study Interfaces.
  2. An abstract class may or may not have an abstract method. But if any class has even a single abstract method, then it must be declared abstract.
  3. Abstract classes can have Constructors, Member variables and Normal methods.
  4. Abstract classes are never instantiated.
  5. When you extend the Abstract class with an abstract method, you must define the abstract method in the child class, or make the child class abstract.

Abstraction using abstract class

Abstraction is an important feature of OOPS. It means hiding complexity. An abstract class is used to provide abstraction. Although it does not provide 100% abstraction because it can also have a concrete method. Let's see how abstract class is used to provide abstraction.
abstract class Vehicle
{
   public abstract void engine();  
}
public class Car extends Vehicle {
    
    public void engine(){
        System.out.println("Car engine");   
        //car engine implementation
    }
    
    public static void main(String[] args)
    {
        Vehicle v = new Car();
        v.engine();
    }
}
Output :
Car engine
Hereby casting an instance of Car type to Vehicle reference, we are hiding the complexity of Car type under Vehicle. Now the Vehicle reference can be used to provide the implementation but it will hide the actual implementation process.

When to use Abstract Methods & Abstract Class?

Abstract methods are usually declared where two or more subclasses are expected to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract methods.
Abstract classes are used to define generic types of behaviors at the top of an object-oriented programming class hierarchy, and use its subclasses to provide implementation details of the abstract class.



























Comments

Post a Comment