top of page

Abstraction

Abstract Methords and Classes

Introduction

Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.

If a class is abstract and cannot be instantiated, the class does not have much use unless it is subclass. This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.

Abstract Methods:

If you want a class to contain a particular method but you want the  actual implementation of that method to be determined by child  classes, you can declare the method in the parent class as abstract. Declaring a method as abstract has two results:

            - The class must also be declared abstract. If a class contains an

               abstract method, the class must be abstract as well.

             

           - Any child class must either override the abstract method or

             declare itself abstract.

A child class that inherits an abstract method must override it. If they do not, they must be abstract, and any of their children must override it.

An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner.

 

 

 

Use the abstract keyword to declare a class abstract.

 

/* File name : Employee.java */

public abstract class Employee{

private String name;

public Employee(String name){

                 this.name = name;

}

public String getName(){

                return name;

}

//abstract method : don't have a defintion

public abstract double getSalary();

}

 

Subtopic Starts:

Extending Abstract Class:

 

We can extend Employee class in normal way as follows:

/* File name : Salary.java */

public class Salary extends Employee

{

         private double salary; //Annual salary

         public Salary(String name, String address, int number, double

         salary)

        {

                   super(name);

                   this.salary=salary

       }

        public double getSalary()

       {

                  return salary;

       }

        public void setSalary(double newSalary)

       {

                 if(newSalary >= 0.0)

                 {

                                 salaiy = newSalary;

                 }

           }

}

 

Here we cannot instantiate a new Employee, But if we instantiate a new Salary object, the Salary object will inherit the three fiels and seven methords from Employee.

 

/* File name : AbstractDemo.java */

public class AbstractDemo

{

              public static void main(String I] args)

                {

                                 Salary s = new Salary("Mohd Mohtashim" ,    3, 3600.00);

                                 System.out.println(s.getSalary());

                }

}

 

This would produce following result:

3600.0 '

 

 

bottom of page