When we use an abstract modifier before the name of any user-define class then it is called as an abstract class. Abstract class in java has only method signature whose implementation must be in its sub-classes.

Important points regrading Abstract class

1. It cannot be instantiate means we cannot create object of an abstract class.
2. An abstract class can have instance member.
3. Abstract class doesn't have any method implementation instead can have only method declaration.
4. We cannot make an inner class as abstract.
5. We cannot use static method but can use static variable in the abstract class.
6. Extending abstract class means we need to override the abstract methods defined in the abstract class.
7. If we don't want to override abstract in its sub-class then declare that sub-class as abstract.
8. An abstract class may or may not have an abstract method.
9. Constructor of the abstract class will be used to instantiate the abstract class instance variable.
10. Overridden abstract class methods will be accessed by its sub-class object.
11. We can be able to create and reference variable for an abstract class.

Why we cannot instantiate an abstract class?
As because we are not implementing abstract method while declaring it in abstract class. So if an user have any chance to create an instance of abstract class means can able to call abstract method with that object but we don't have any body while declaring it in abstract so no need of calling an empty body method. That's why we are not allowed to create an instance of an abstract class.

If we are not allowed to create and instance of an abstract class then how it can have an instance variable?
We can be able to access instance member of any super-class with its sub-class object therefore we can be able to access the instance member of any abstract class with the object of its sub-class.

Who is responsible to initialize the instance member of abstract class and how?
Its obvious that constructor is always responsible to initialize any instance member of any class. Here by creating its sub-class object will call its constructor and with the super implementation in it, will be able to class its super-class constructor i.e abstract class constructor and finally that constructor is responsible to initialize its instance member.

Why we cannot use static keyword with the abstract method?
By using static keyword with the abstract method will make it eligible to call it with its class name but no need of calling an empty body method. So it is restricted to use static with the abstract method.

Why we need to override abstract method in its sub-class?
It is because an user can be able to call abstract method with its sub-class object so we need to override so that it will call an overridden implemented method.

What will happened if we are extending an abstract class with another abstract class?
Then no need to override the abstract method in its sub-class because sub-class itself is an abstract class.

Why we can create an reference variable of an abstract class?
It is because creating an reference variable doesn't instantiate the member of the abstract class and hence contain null means not referring any object.

Use of an abstract class

1. When we have only static member in our class which can able be accessed with its class name so no need of creating object unnecessarily.So for restricting user to create an object declare that class as abstract.

2. It is used to when the user want to have their own implementation. So declaring a method as abstract will give a chance to an user to have their own implementation for same method name.

An example showing an abstract class with some valid and invalid statement.

abstract class A{

 static int x;                 // valid
 int y;                       // valid
 abstract void add();         // valid
 static abstract void sub(); // invalid as static is not allowed.
}
public class E extends A {

 void add(){}       //Compulsory to override abstract method 
 
 public static void main(String[] args) {
  E e = new E();
  System.out.println(A.x);
  A a = new A();        // Invalid 
 }
}

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic