Abstraction in Java

 Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.

Abstraction in Java
Abstraction in Java

For example, when you login to your Facebook account online, you enter your user_id and password and press login, what happens when you press login, how the input data sent to Facebook server, how it gets verified is all abstracted away from the you.

hierarchy abstract class
hierarchy abstract class

Abstract class in Java :


A class which is declared as abstract is known as an abstract class. It can have abstract methods (methods without body) as well as concrete methods (regular methods with body). A normal class (non-abstract class) cannot have abstract methods.

Abstract Class Declaration :
//Declaration using abstract keyword
abstract class A
{
//This is abstract method
abstract void myMethod();

//This is concrete method with body
void anotherMethod()
{
//Does something
}
}

Imp Points to Remember :

  • An abstract class must be declared with an abstract keyword.
  • It can have abstract and non-abstract methods.
  • It cannot be instantiated (you are not allowed to create an object of it)
  • It can have constructors and static methods also.
  • It can have final methods which will force the subclass not to change the body of the method.
Rules for abstract class
Rules for abstract class


Realtime use of Abstract class :

Lets say we have a class Vehicle that has a method VehicleCompany() and the subclasses of it like Tata, Mahindra, BMW, Audi etc. Since the Vehicle Company differs from one Vehicle  to another, there is no point to implement this method in parent class. This is because every child class must override this method to give its own implementation details, like Tata class will say “vehicle company is Tata” in this method and BMW class will say “Vehicle company is BMW”.

So when we know that all the vehicle child classes will and should override this method, then there is no point to implement this method in parent class. Thus, making this method abstract would be the good choice as by making this method abstract we force all the sub classes to implement this method( otherwise you will get compilation error), also we need not to give any implementation to this method in parent class.

Since the Vehicle class has an abstract method, you must need to declare this class abstract.

Now each vehicle must have a company name, by making this method abstract we made it compulsory to the child class to give implementation details to this method. This way we ensures that every vehicle has a company.

Example :
//abstract parent class
abstract class Vehicle
{
//abstract method
public abstract void VehicleCompany();
}
//BMW class extends Vehicle class
public class BMW extends Vehicle
{

public void VehicleCompany()
{
System.out.println("Vehicle company is BMW");
}
public static void main(String args[])
{
Vehicle obj = new BMW();
obj.VehicleCompany();
}
}

Output :
Vehicle company is BMW



Why can’t we create the object of an abstract class?


Because these classes are incomplete, they have abstract methods with no body; hence, if Java allows you to create objects of this class, what happens if someone calls the abstract method using that object? 


There would be no way to call the method because it would not be implemented. An abstract class is like a template, so you have to extend it and build on it before you can use it.


Example of Abstract class and method :


abstract class TestClass 
{
public void show()
{
System.out.println("Concrete method of parent class");
}
abstract public void disp2();
}

class Demo extends TestClass
{
/* Must Override this method while extending
* TestClas
*/
public void show2()
{
System.out.println("overriding abstract method");
}
public static void main(String args[])
{
Demo obj = new Demo();
obj.show2();
}
}

Output :
overriding abstract method



in Next Article we learn about Interface




Tech Smart

Hello, my name is Sam and I am the blog's author. I enjoy blogging since I am passionate about it and I am an enthusiastic learner. Blogging provides me a greater platform to share all of my knowledge and experiences with my readers.

Post a Comment

if you have any doubts, Please let me know

Previous Post Next Post