Abstract Class

What is abstraction ?

In java, Abstraction means to show only functionality and hide complexity or internal details or hide implementation details to the user is know as abstraction in java. E.g. ATM machine.

How to achieve abstraction in java?

There are two ways in java we can achieve abstraction.

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

  • By using interface(100%).

What is abstract classes and why it is used ?

A class which is declare with abstract keyword is called abstract class having properties :

  • An abstract class cannot be instantiated directly , only its subclasses can be instantiated.

  • A class that has one or more abstract methods must be declared abstract.

  • It have abstract and non abstract method both.

  • Each subclass of an abstract class must override the abstract methods of its super classes.

  • A subclass that does not provide an implementation for its inherited abstract methods must also be declared abstract.

abstract class Sample {   
public int a =10 ; // can be static , private , final 
public abstract void show();    
public void display()  {    
    Sysout(“hii”);  
    }
}

What are abstract methods ?

An abstract method is a method without body. i.e. its having no implementation.

  • Declare method, using abstract keyword.

  • All method declared inside Java Interface are by default abstract .

  • public void abstract printVersion();

  • Now, In order to implement this method, you need to extend abstract class and override this method.

Why to use abstract class over interface ?.💥💥

These are some of the differences -

  • Abstract classes are useful in a situation when some general methods is implemented and specialization behavior should be implemented by subclasses.

  • Interfaces are useful in a situation when all its properties need to be implemented by subclasses.

  • If we want to provide default implementations to some methods then it is good to go for abstract class .

  • If we add a new method on a published interface all its implementation must implement it , it’s better to use abstract class

  • If there are too many methods inside interface then it will create performance issue for implementation so its better to use abstract class .

If interface & abstract class have same methods and those methods contain no implementation, which one would you prefer?

Obviously as Implementing an interface for a class is very much effective rather than extending an abstract class because we can extend some other useful class for this subclass.

Why abstract does not provide 100% abstraction .

Because it contains both abstract and non abstract or concreate methods so they can’t be 100% abstract like interfaces

Can we create instance of abstract class?

No, We cannot create an instance of an abstract class. As it contains abstract class that subclass should implement .

Difference between abstract class and interface .💥💥

Interface

Abstract Class

If we don't' know anything about implementation just we have requirement specification then we should go for interface

If we are talking about implementation but not completely (partial implementation) then we should go for abstract class

All methods inside interface are implicitly abstract. Interface helps to achieve 100% abstraction

Abstract class can contain both abstract method and concrete method

Only public or default(package) is allowed

Can have any visibility

Every method present inside interface is always public and abstract whether we are declaring or not.

Every method present inside abstract class need not be public and abstract. It can be protected also

We can't declare interface methods with the modifiers private, protected, final, static, synchronized, native, strictfp.

There are no restrictions on abstract class method modifiers

Every interface variable is always public static final whether we are declaring or not.

Every abstract class variable need not be public static final

Variables must be initialized at time of declaration

Not required

Inside interface we can't take static and instance blocks.

Inside abstract class we can take both static and instance blocks

Inside interface we can't take constructor.

Inside abstract class we can take constructor

No implementation to methods till java 8

May have unimplemented methods

Inherit from Object

Not inherit from object so can’t override equals() , clone() .. etc

What modifiers are applicable for abstract class ?

Applicable : Public , protected

Not Applicable : Final , private

What modifiers are applicable for methods in abstract class ?

Applicable : Public , protected

Not Applicable : private , static , final

What modifiers are applicable for variables in abstract class ?

Public, private , protected , final ,static

What keywords are not applicable for abstract keywords ?

Final ,static ,synchronised , native , strictfp , private

Can we declare abstract method in non-abstract class?

No, either it should be abstract class or interface .

Can we declare abstract method as static?

No, We can't use static keyword with abstract method.

Can we declare abstract method as final?

No, because abstract methods needs to be implemented , but we declare them as final them we can’t implement them .

Can we declare abstract method as private?

No, because it needs to be implemented .

Can we declare local inner class as abstract?

Yes, We can declare local inner class as abstract.

Can abstract class implements interface in java?

Yes, Abstract class can implement interface.

Can we use “abstract” keyword with constructor, Instance Initialization Block and Static Initialization Block?

No, Constructor, Static Initialization Block, Instance Initialization Block and variables can’t be abstract.

Abstract classes can be nested. true or false?

Yes, Abstract classes can be nested.

Can abstract class have constructor in java?

Yes, We can declare constructor in abstract class.

We can’t instantiate an abstract class. Then why constructors are allowed in abstract class?💥💥

  • It is because, we can’t create objects to abstract classes but we can create objects to their sub classes.

  • From sub class constructor, there will be an implicit call to super class constructor. To initialize variables inside abstract class.

  • Even if you don’t write constructor for your abstract class, compiler will keep default constructor.

Can abstract method declaration include throws clause?

Yes.

What will happen if we do not override all the abstract methods in sub-class?

It will throw compile-time error. If you do not want to override all the abstract method in sub-class then you have to make your sub-class as abstract class.

Can we define abstract class without abstract method?

Yes we can define abstract class without abstract methods

Can we create object for abstract class?

Abstract class can not be instantiated directly. Through sub class abstract class members will get memory. Whenever we create sub class object of abstract class abstract class members will get memory at that time.

Can we declare abstract methods as synchronized?

No, abstract methods can not be declared as synchronized. But methods which override abstract methods can be declared as synchronized.

Can abstract class implements interface in Java? does they require to implement all methods?

Ans: Yes, abstract class can implement interface by using implements keyword. Since they are abstract, they don’t need to implement all methods.

Can abstract class have static methods in Java?

Yes, abstract class can declare and define static methods but it is not recommended as static method can’t be overridden .

Can abstract class contains main method in Java ?

Yes, abstract class can contain main method, it just another static method and you can execute Abstract class with main method

What is the difference between final and abstract ?

  • For abstract methods compulsory we should override in the child class to provide implementation. Whereas for final methods we can't override hence abstract final combination is illegal for methods.

  • For abstract classes we should compulsory create child class to provide implementation whereas for final class we can't create child class. Hence final abstract combination is illegal for classes.

  • Final class cannot contain abstract methods whereas abstract class can contain final method.

What is the difference between interface, abstract class and concrete class? Or When we should go for interface, abstract class and concrete class? 💥💥

  • If we don't know anything about implementation just we have requirement specification then we should go for interface.

  • If we are talking about implementation but not completely (partial implementation) then we should go for abstract class.

  • If we are talking about implementation completely and ready to provide service then we should go for concrete class

Last updated