এবস্ট্রাক্ট মডিফায়ার [ Abstract Modifier ] | Object Oriented Programming

এবস্ট্রাক্ট মডিফায়ার [ Abstract Modifier ] ক্লাসটি পলিটেকনিক [ Polytechnic ] এর অবজেক্ট ওরিয়েন্টেড প্রোগ্রামিং (বিষয় কোডঃ ৬৬৬৪১) (Object Oriented Programming Code 66641) বিষয় এর অংশ।

 

এবস্ট্রাক্ট মডিফায়ার [ Abstract Modifier ]

 

The abstract modifier in Java is applicable for classes and methods but not for variables. It is used to achieve Abstraction, one of the key concepts in Object-Oriented Programming.

এবস্ট্রাক্ট মডিফায়ার

Abstract Methods

Even if we don’t know the implementation, we can still declare a method with the abstract modifier, which means that only declaration, not implementation, is available for abstract methods. As a result, abstract methods should be terminated by a semi-colon.

public abstract void m1();
public abstract void m2() {}

In the above code snippet, the declaration of method m1() is correct whereas that of m2() is incorrect.

The child class(the class that extends the parent class) is responsible for providing the implementation of parent class abstract methods.

Consider a Vehicle as a real-world application of abstract methods. Varied vehicles have different numbers of wheels, such as a car with four wheels, an auto with three wheels, and so on. Let us create a Vehicle class and define a getNumberOfWheels() method inside it.

public abstract class Vehicle {
    public abstract int getNumberOfWheels();
}class Car extends Vehicle {
    @Override
    public int getNumberOfWheels() {
        return 4;
    }
}class Auto extends Vehicle {
    @Override
    public int getNumberOfWheels() {
        return 3;
    }
}

We provide instructions to the child class by declaring the method abstract in the parent class that these methods must be implemented by the child class.

Note:

Abstract methods never talk about implementation. If any modifier talks about implementation, then it forms an illegal combination with the abstract modifier.

The following are various illegal combinations of modifiers for methods with respect to the abstract modifier.

For example:

public abstract class Test {
    abstract final void m1();
}

Output:

Abstract Class

If we are not allowed to construct an object for any Java class (due to partial implementation), we must declare such classes with the abstract modifier, i.e., object instantiation is not possible for abstract classes.

public abstract class Test {
    public static void main(String[] args) {
        Test t = new Test();
    }
}

Output:

Conclusions about Abstract Class and Abstract Method

 

  • If a class has at least one abstract method, it must be declared as an abstract class; otherwise, a compile-time error will occur.
  • If a class contains at least one abstract method, then its implementation is incomplete and hence we cannot create objects of that class. To restrict object creation, the class must be declared as an abstract class.
  • Even if a class doesn’t have any abstract methods, we can designate it as an abstract class if we want to restrict object creation. An abstract class can have zero abstract methods.
    e.g.HttpServlet class is abstract but it doesn’t contain any abstract method.

এবস্ট্রাক্ট মডিফায়ার [ Abstract Modifier ] এর বিস্তারিত ঃ

Leave a Comment