Top Ad unit 728 × 90

Welcome !!! To The World of Programming

OOPS FEATURES

Oops

Object Oriented Language - Abstract Class


 Abstract Class

A class is a user-defined data type. It is used to model an identified Entity in the problem domain. The information required of an entity becomes fields or data members of the class, The actions required of the entity becomes member functions or methods of the class. 

  • It allows to define methods and properties that must be implemented by any concrete (i.e., non-abstract) subclass.
  • It is used to create a common interface
  • To provide some default implementation while leaving specific details to the derived classes.
  • The primary purpose of abstract classes is to define a common interface and a default behavior for a group of sub classes.

  • It ensures that all subclasses adhere to a specific contract while allowing for variations in the implementations.
  • It cannot be instantiated on its own.
  • It is used as a blueprint or template for sub classes.



Abstract Class

However, in some situation, It is not possible to provide definition to one or more methods of the class. Let us understand the concept in the following scenario.

The identified Entity is Two-dimensional figure. It has two dimensions. Its area is required to be computed. Triangle, Rectangle, Square, Circle etc. are all two-dimensional figures. However the formula to compute area is different for each of the figures. The identified  entity can be modelled in any object-oriented language as a class as shown below:


class TwoDFigure {
    //Fields to store dimension of the two-dimension figure 
    private int d1;
    private int d2;
    //Constructors to initialize the dimensions
    public TwoDFigure(int d1,int d2) {
        this.d1 = d1;
        this.d2 = d2;
    }
    //Method to compute the area of the figure
    public void computeArea() {
       //Cannot be implemented here
       //Cannot write the code to compute the area
    }
}

The problem is that the developer is unable to provide definition to the computeArea() method of the class. A method that  can be defined is termed as concrete method and method that cannot be implemented in the class is termed as an abstract method. The class is termed as an Abstract class.

Abstract Method:

A method that cannot be defined or implemented in the class.

Abstract Class:

A class that has abstract method.

Features:

  • It can also have a concrete method.
  • It cannot be instantiated. It means the object or instance of the class cannot be created. 
  • It makes inheritance compulsory.
  • It also makes Overriding compulsory
Recall:

A class can be used in two ways:

  • By creating object or an instance of the class.
  • By Inheriting the class.

As a developer or a programmer, one requires the knowledge of defining the abstract class and abstract method in the selected object-oriented language. The next task is to explore how an abstract class and abstract method is implemented in various object oriented languages.


Think and Try:

A three-dimensional figure is the identified entity to be modelled as a class. The information required of the entity are the three dimensions of the 3-d figure. Think of adding a constructor to initialize the dimensions of the figure. The actions required of the entity are the methods to compute the Volume, Lateral Surface Area and Total Surface Area of the three-dimensioned figure. (Cylinder, Cone, Cuboid, Cube and Sphere.
Object Oriented Language - Abstract Class Reviewed by Syed Hafiz Choudhary on September 12, 2023 Rating: 5

No comments:

Contact Form

Name

Email *

Message *

Powered by Blogger.