Home / Computer Science / Java Language / Abstraction in Java
Abstraction in Java

Abstraction in Java

Abstraction refers to the act of representing essential features to the outside world and showing the necessary functionality.  Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.

For better understanding of abstraction gone through the below examples:

  1. The best example in the world of abstraction is ATM machine where we can withdraw or transfer money easily but the user doesn’t know the internal implementation details but the functionality is happens.
  2. Another classical example the Abstraction is a car. We can regard the interface as the dashboard, pedals, and shifter. The implementation is the engine and transmission. Think about how difficult it would be if drivers needed to understand engines in order to drive and had to relearn to drive each time a mechanic replaced an engine part.

The abstraction can be achieved by java in two ways.

  • By using abstract class (0 to 100%).
  • By using interface (100%).

What is an abstract class in Java?

A class is an abstract class, when we declared any class with “abstract” keyword is known as abstract class. The class may contain abstract method or non abstract method. i.e method with body or without body.

abstract class A{

abstract demoMethod();

}

Here class and methods are declared with abstract key word.

The important point to remember is the abstract class cannot be instantiated, i.e we cannot create instance for the class.

Abstract classes are not complete; they are just defining a function for service, from that base construct the derived class.

Can we create instance for abstract class? Simply, No.

Abstract classes cannot be instantiated, means we can’t create an object to Abstract class. We can create Subclasses to Abstract classes. An Abstract class may or may not have abstract methods, abstract method in the sense a method can declared without any body implementation is called abstract method.

Is it possible to create abstract method in non-abstract class?

No, it throws an error.

Class Demo{

abstract demo(){

}

}

Abstract classed can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

Abstract classes can be initialized, when we define a class to be an Abstract Class it cannot be instantiated but that does not mean an Abstract class cannot have a constructor. Each abstract class must have a concrete subclass which will implement the abstract methods of that abstract class.

Key points about abstraction:

  1. We cannot declare abstract method as final.
  2. We cannot declare abstract method as private.
  3. We cannot create instance for interface.
  4. An abstract class is a class that is declared with abstract keyword.
  5. An abstract method is a method that is declared without an implementation.
  6. An abstract class may or may not have all abstract methods. Some of them can be concrete methods
  7. A method defined abstract must always be redefined in the subclass, thus making overriding compulsory OR either make subclass itself abstract.
  8. Any class that contains one or more abstract methods must also be declared with abstract keyword.
  9. There can be no object of an abstract class. That is, an abstract class can not be directly instantiated with the new operator.
  10. An abstract class can have parameterized constructors and default constructor is always present in an abstract class
  11. All the methods in an interface are implicitly abstract unless the interface methods are static or default.
  12. Java Abstract class is used to provide common method implementation to all the sub classes or to provide default implementation.
  13. We can run abstract class in java like any other class if it has main() method.

Demo program for abstraction:

package santosh;

abstract class Bird 
{
public abstract void fly();
}
class Parrot extends Bird
{
@Override
public void fly() 
{
System.out.println("parrot fly..");
}
public void eat(){
System.out.println("parrot is eating...");
}

}
class Sparrow extends Bird
{
@Override
public void fly() 
{
System.out.println("sparrow flying");
}

}
public class DemoAbstract {
public static void main(String args[])
{
Bird b = new Parrot();
b.fly();
Bird b1= new Sparrow();
b1.fly();
System.out.println("object is "+b1.getClass());// returns the runtime class of this object
}
}

Output of the above program:

parrot fly..
sparrow flying
object is class santosh.Sparrow

Abstraction in java with constructor:

An abstract class can have constructor, abstract methods, non-abstract methods.

package santosh;

abstract class Bird 
{
Bird(){//constructor
System.out.println("Bird constructor called");

}
public abstract void fly();
public void color()
{
System.out.println("color is green");
}
}
class Parrot extends Bird
{
@Override
public void fly() 
{
System.out.println("parrot fly..");
}
public void eat(){
System.out.println("parrot is eating...");
}

}
class Sparrow extends Bird
{
@Override
public void fly() 
{
System.out.println("sparrow flying");
}

}
public class DemoAbstract {
public static void main(String args[])
{
Bird b = new Parrot();
b.fly();
Bird b1= new Sparrow();
b1.fly();
System.out.println("object is "+b1.getClass());// returns the runtime class of this object
}
}

Output of the above program:

Bird constructor called
parrot fly..
Bird constructor called
sparrow flying
object is class santosh.Sparrow

About Santosh Kumar Gadagamma

I'm Santosh Gadagamma, an Experienced Software Engineer passionate about sharing knowledge in technologies like Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts, and all levels of software design, development, deployment, and maintenance. I believe computers are essential for the world's functioning, and I'm committed to helping others learn the skills they need to succeed in tech. My website is a valuable learning tool to help you reach greater heights in your education and career, and I believe that education has no end points.

Check Also

StringBuffer in Java

A string buffer is similar to String, but the difference is string cannot be modified …