Abstract in Java

 Abstract in Java

  

In Java, an abstract is a concept related to classes and methods that allows you to define incomplete or partially implemented structures. 

It's used to create a blueprint for other classes to extend or implement.


Types of abstracts in Java: 

1.abstract classes 

2.abstract methods.


1. Abstract Class:

An abstract class in Java is a class, which is declared with an abstract keyword. 

It is just like a normal class but has two differences.

1. We cannot create an object of this class. Only objects of its non-abstract (or concrete) sub-classes can be created.

2. It can have zero or more abstract methods allowed in a abstract class .

3 .Abstract class represents an incomplete class that depends on subclasses for its implementation. Creating subclass is compulsory for abstract class.

4. A non-abstract class is sometimes called a concrete class.

5. An abstract concept is not applicable to variables.


Use Abstract class in Java?

  • An abstract class can be used when we need to share the same method to all non-abstract subclasses with their own specific implementations.
  • abstract class is useful to make the program more flexible and understandable.



2. Abstract Method:

  • An abstract method is a method declared in an abstract class but does not have any implementation.
  • It is marked with the "abstract" keyword and ends with a semicolon, without a method body.
  • Abstract method does not contain any body. Therefore, it is also known as incomplete method in java.
  • It compulsary  to provide (body) concrete implementations for all the abstract methods of abstract class by inheriting in subclass.
  • A concrete method is a method which has always the body. It is also called a complete method in java.
  • If any method is abstract in a class, the class must be declared as abstract.
  • A non-abstract class cannot have an abstract method, whether it is inherited or declared in Java.
  • It must not provide a method body/implementation in the abstract class for which it is defined.
  • Abstract method cannot be static or final.
  • It cannot be private because the abstract method must be implemented in the subclass. If we declare it private, we cannot implement it from outside the class.  


Use Abstract method in Java

1. An abstract method can be used when the same method has to perform different tasks depending on the object calling it.

2. A method can be used as abstract when you need to be overridden in its non-abstract subclasses.


Example of an Abstract Class:

abstract class Shape {

    // Concrete method

    public void display() {

        System.out.println("This is a shape.");

    }


    // Abstract method

    public abstract double area();

}



Features of Abstract class in Java

1. Abstract class is not a pure abstraction in java.

2. In Java, object creation is not possible for an abstract class because it is a partially implemented class, not fully implemented class.

3. Abstract class allows to define private, final, static and concrete methods. 

4. It can have constructors.

5.  Abstract class does not support multiple inheritance in java but allows in interfaces.

6. It can implement one or more interfaces in java but extend single class only.



abstract classes are an essential part of Java's object-oriented programming paradigm, providing a powerful mechanism for building flexible and maintainable class hierarchies.


Comments

Popular Posts