Abstract Class in Java

By | April 12, 2015
Before reading this tutorial I will recommend you to read
about dynamic binding in java, because without knowing about dynamic binding
you can’t understand the concept of abstract class in java.


Abstraction in Java

  • Abstraction is the process of hiding the complexity and
    showing the functionality. Showing the essential data and hiding the non
    essential data. 
  • We can understand this by taking a real world example. We
    have fan in house, we are just provided with a switch (functionality) to turn
    on and off the fan, we are not aware of how the fan actually works
    (complexity). This is a perfect example of abstraction in java.

Abstraction in java is achieved by two ways:
1. Abstract Class (Partial implementation of abstraction)
2. Interface (Fully implementation of abstraction)

Abstract Class in Java

  • A class which has zero or more abstract methods is known as
    abstract class. It can also have non abstract methods. 
  • Abstract class can’t be instantiated. It means we can’t
    create object of an abstract class. However we can create its reference variable.

Abstract Method in Java

Abstract method is a method without body. Abstract class and
abstract method is declared using “abstract” keyword. Below is an example.

When we derive a class from an abstract class then we have
to give body of all the abstract methods in the derived class. If we don’t give
body of methods then we have to make the derived class as abstract class.
Below is one program example that implements the concept of
abstract class in java.

Abstract Class Example


In above example, task is performed depending upon the
object calling it. As I have already told you that abstract class can’t be
instantiated. So in above program we are just declaring a reference variable of
abstract class Base and storing the reference id of derived class in it, this
concept is called as dynamic binding.

Lets talk about few important interview questions for
abstract class in java

Interview Questions

Q. Why we use dynamic binding?
A. To achieve abstraction.
Q. How we execute parent class methods?
A. Using dynamic binding. This is implemented in above
Q. Can we keep construct in abstract class?
A. Each class in java have constructor so abstract class
also have constructor.
Q. Can we make abstract method static?
A. No, because static is accessed via class name and it will
be accessed by anyone.
This was all about abstract class in java. If you find anything
missing, incorrect or have doubts regarding above tutorial then please mention
it by commenting below.

Leave a Reply

Your email address will not be published. Required fields are marked *