Core Java:Abstraction interview question

1.What is an abstract class?

Abstract classes are classes that contain one or more abstract methods. An abstract method is having only declaration, but contains no implementation.
Importat point :-

  • If even a single method is abstract, the whole class must be declared abstract.
  • Abstract classes cannot be instantiated, and the class that is extending a Abstract class need to provide implementations for the abstract methods.
  • You can’t mark a class both abstract and final.
  1. Can we instantiate an abstract class?

An abstract class can never be instantiated. Its sole purpose is to be extended by some other class that can provide implementations for the abstract methods.

  1. What are the differences between Interface and Abstract class?
Abstract Class Interfaces
An abstract class can provide complete, default code and/or just the details that have to be overridden. An interface cannot provide any code , just the signature.
In case of abstract class, a class can extend only one abstract class. A Class may implement several interfaces.
An abstract class can have abstract as well non-abstract methods. All methods of an Interface are abstract.
An abstract class can have instance variables. An Interface cannot have instance variables.
An abstract class can have any visibility: public, private, protected. An Interface visibility must be public (or) none.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
An abstract class can contain constructors . An Interface cannot contain constructors .
Abstract classes are fast. Interfaces are slow as it requires extra indirection to find corresponding method in the actual class.

4.When should we use abstract classes and when should I use interfaces?

Use Interfaces when…

1.You see that something in your design will change frequently.

2.Another general rule is if you are creating something that provides common functionality to unrelated classes, use an interface

  1. when you don’t want a massive hierarchical type framework. This isn’t my example, but its the best one I have come across. Lets say you have an interface for a Director and another interface for a Actor.

 

public interface Actor{

Performance say(Line l);

}

public interface Director{

Movie direct(boolean goodmovie);

}

In reality, there are Actors who are also Directors. If we are using interfaces rather than abstract classes, we can implement both Actor and Director. We could even define an ActorDirector interface that extends both like this:

 

public interface ActorDirector extends Actor, Director{

}

We could achieve the same thing using abstract classes, but that will lead in a complex hierarchy of classes.

 

 

Use Abstract class when…

 

  1. Abstract classes allow you to provide default functionality for the subclasses. If you plan on updating base class throughout the life of program, it is best to make that base class to be an abstract class. Because you can make a change to it and all of the inheriting classes will now have this new functionality. If the base class is changing often and an interface was used instead of an abstract class, we are going to run into problems. Once an interface is changed, any class that implements that will be broken.

 

 

  1. 2. While creating a generic List object, we should use abstract class. Every single List object is going to display the data in a list in some form or another. If we want to change that List object, we just extend it, override our build list function, change what we want and call super.buildList ();

5.When you declare a method as abstract, can other nonabstract methods access it?

Yes, other nonabstract methods can access a method that you declare as abstract.
6.Can there be an abstract class with no abstract methods in it?

Yes, there can be an abstract class without abstract methods.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.