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.
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.
abstract method is declared using “abstract” keyword. Below is an example.
abstract class Demo //abstract class { abstract void add(int a, int b); //abstract method . . . . . . . . . . }
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.
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 in java.
Abstract Class Example
abstract class Base { abstract int calculate(int a); void display() { System.out.println("Base"); } } class Square extends Base { int calculate(int a) { return a*a; } } class Cube extends Base { int calculate(int a) { return a*a*a; } } class Demo { public static void main(String...s) { Base b1=new Square(); Base b2=new Cube(); System.out.println(b1.calculate(2)); //will call calculate() of Square class System.out.println(b2.calculate(2)); //will call calculate() of Cube class b1.display(); //will call method of Base class } }
Output
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
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
program.
program.
Q. Can we keep construct in abstract class?
A. Each class in java have constructor so abstract class
also have constructor.
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.
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.
missing, incorrect or have doubts regarding above tutorial then please mention
it by commenting below.