Abstract Class in Java

Abstract Class in Java


In Java programming language a class that is declared by using the keyword abstract is known as abstract class. An abstract class in java programming language can have abstract methods and non abstract methods also.


Abstraction in Java:

Abstraction is a process in which we are exposing the interfaces and hiding the implemented data or details from the user. A pure virtual function is necessary or it is a condition for a class to be abstract. An abstract class can have more than one pure virtual function. If a class has more than one function, the derived class must define every pure virtual function in its base class; if not so it will also be an abstract class. We can also create our own data types using data abstraction and then define objects of these data types. Through data abstraction the user can define the set of operations on their own created data types. Data abstraction is also used to easily modify the program.

Data abstraction is also used to hide the data from the user or to hide the data of the structure. The data can be hid by using the access specifiers. There are three types of access specifiers in object oriented programming; private, protected, and public. Usually the data members in a class are kept private and in the parent class or the base class the data members are kept protected. The member functions or methods are always public, so that they can be used anywhere in the scope of the program.

In short, data abstraction is a concept that only shows the necessary details to the user and hides the data.

Abstract class and interface is used to achieve abstraction in Java programming language.

Abstraction is a concept in which important things are visible to the user and the internal details are hidden. A real life example is that when we send a message through mobile only the message is visible to the user and the internal process is invisible to the receiver as well as to the sender.

Through abstraction we can determine the working of the object.


Ways to achieve Abstraction:

In Java programming language there are two different ways through which we can achieve abstraction:

  1. Abstract class
  2. Interface


Abstract class in Java:

In Java programming language a class that is declared by using the keyword abstract is known as abstract class. An abstract class in java programming language can have abstract methods and non abstract methods also.


Example of abstract class:

The following line of code demonstrates that how an abstract class can be declared:

abstract class B { }


Abstract method:

In java programming language an abstract method is the one which is declared as abstract by using the abstract keyword. The abstract method does not have any implementation.


Example of abstract method:

Consider the following line of code in which an abstract method is declared:

abstract void display ();


Example of abstract class that has abstract method:

Consider the following example in which the parent class is an abstract class and has one abstract method and the implementation of this abstract method is provided by the child class:

CODE:

abstract class Vehicle {

abstract void display ();

}

class Car extends Vehicle {

void display () {

system. out. prinln (“Vehicle is running”);

}

public static void main (string args []) {

Vehicle v1 = new Car ();

v1. display ();

}

}

OUTPUT:

Vehicle is running

In the above example the parent class that is Vehicle is an abstract class and it has an abstract method named display (). The abstract method display () is declared only in the abstract parent class. The definition of the abstract method display () is provided in the child class that is Car.


Understanding the real scenario of abstract class:

Consider the following example in which there is an abstract parent class known as Shape and the implementation of this class is provided by the subclasses that is Square and Rectangle. The implementation of this is hidden to the user and the object for this class is provided by the factory method.

In Java programming language a factory method is a method which is used to return the instance of the current class.

Consider the following example:

CODE:

abstract class shape {

abstract void sketch ();

}

class Square extends Shape {

void sketch () {

system. out. println (“Drawing Square”);

}

}

class Rectangle extends Shape {

void sketch () {

system. out. println (“Drawing Rectangle”);

}

}

class CheckingAbstraction {

public static void main (string args []) {

Shape s1 = new Square ();

s1. sketch ();

}

}

OUTPUT:

Drawing Square

In the above example we created the instance or the object of the square class to call or invoke the method sketch () of the square class.


Another example of abstract class in Java:

Consider that you have a class that is named as Animal and now you have three child classes that fall in the category of animal. Now in all of the child classes you have a display () method that will have the same implementation and that will have same parameters.

CODE:

abstract class Animal {

abstract void display () {

system. out. println (“Animals that are mammal”);

}

}

class Cat extends Animal {

void display () {

system. out. println (“Cats are mammal”);

}

}

class Dog extends Animal {

void display () {

system. out. println (“Dogs are mammal”);

}

}

class Lion extends Animal {

void display () {

system. out. println (“Lions are mammal”);

}

}

class ABC {

public static void main (string [] args) {

Animal a;

a = new Cat ();

a = new Dog ();

a = new Lion ();

a1. display ();

a2. display ();

a3. display ();

}

}

OUTPUT:

Cats are mammal

Dogs are mammal

Lions are mammal

In the above example Animal is the parent class and cat, dog, and lion are the child classes. We used method overriding in the above program and the method display () is overridden.

The parent class is declared as an abstract class and it has an abstract method also named display ().


Abstract class having constructors, data members and methods, etc:

In Java programming language an abstract class can have data members, abstract methods and method body and constructors also. In an abstract class in java programming language can also have a main () method in it.

Consider the following example in which the abstract class has an abstract method:

CODE:

abstract class Car {

Car () {

system. out. println (“Car is running”);

}

abstract void display ();

void gear () {

system. out. println (“Gear of car is changed”);

}

}

class Company extends Car {

void display () {

system. put. println (“Car is running safely”);

}

}

class ABC {

public static void main (string args []) {

Car c1 = new Company ();

c1. run ();

c1. gear ();

}

}

OUTPUT:

Car is running

Gear of car is changed

Car is running safely

In the above example the abstract class is Car and it has an abstract method whose implementation is done in the sub class. It should be noted that to declare an abstract method the class in which it is declared should also be abstract.

Consider the following example in which we got a compilation error when the abstract method is declared in a class which was not abstract:

CODE:

class Car {

abstract void display ();

}

OUTPUT:

Compile time error

If an abstract class is extended that has an abstract method also then the implementation of the method should be provided.


Another real scenario of abstract class:

In java programming language we can also use the abstract class to provide the implementation of the interface. In this way the user at the end will not have to override all the methods of the interface.

We will discuss example for this later after discussing interface in Java.