No comments yet

Top 15 Interview Questions in Abstraction

[Social9_Share type="horizontal"]

Top 15 Interview Questions in Abstraction

In this blog lets discuss about the interview questions relevant to Abstraction

  1. What is Abstraction?
  2. Abstraction is one of the Object Oriented Programming concept. It helps in hiding the implementation details from the user.

    Example1:- When you want to drive a car, You are not bothered about how the wheel or brake is connected to the accelerator, or how the engine works. You must know just how to drive the car. When you press the brake, the car must stop. When you press the accelerator, the car must move forward. You are not bothered about the internal working of the engine, which is hidden from you. This is called Abstraction.

    Example2:- In todays world, everybody uses Mobile phone. Any layman using it, is not bothered about how the phone works, or how internet or GPS works. For him, when he makes a call, it must connect and work fine. This is Abstraction – hiding the implentation details

  3. How is abstraction achieved in Java?
  4. Abstraction is achieved with the help of abstract classes and interfaces.

  5. What is an abstract method?
  6. An abstract method is one which has only declaration and no definition.The keyword abstract makes the method abstract.The abstract method should be implemented in the subclasses.
    eg. public abstract double calcBonus();

  7. What is an abstract class?
  8. An abstract class is a class which may or may not have (You can also declare a class abstract even without abstract methods)abstract method. An abstract class must be extended to provide functionality for the abstract methods.The keyword abstract makes the class abstract.
    eg. public abstract class Employee(){}

    Know more about an Abstract class

    • If a class has abstract method, make the class as abstract
    • An abstract class cannot be instantiated, (ie)cannot create objects for an abstract class
    • An abstract class must be extended to provide functionality for the abstract methods
    • The subclasses provide the functionality for the abstract methods
    • If the subclass does not provide functionality, then make the subclass abstract and extend it. This continues till all the abstract methods are implemented properly
    • The abstract class can also have concrete methods


  9. Can an abstract class have private methods?
  10. Yes. An abstract class can have private methods. But it can be used only from within the abstract class.

  11. How to call the overridden abstract method?
  12. Create a reference of the super class and make it point to any of the subclass object. Then, when you call the method using the super class reference, only the overridden method in the subclass will be called. Based on the above example, we can write the java code as follows.

    Always call the overridden method in the subclass using the super class reference only.

    Become an expert Java Developer

  13. How to call the other concrete methods of the super class?
  14. The other concrete methods can be called using the same super class reference.The code is given below, based on the above example.

  15. How to call the own methods in the subclass?
  16. To call the own methods in the subclass, downcast the super class reference to sub class reference. Then, call the subclass methods.The code is given below, based on the above example.

  17. When does ClassCastException occur?
  18. Lets use the same example as given above – Employee as super class, Manager and Programmer as subclasses. calcBonus() is overridden in the subclass Manager and Programmer.

    So, when does ClassCastException occur.
    Create an Employee reference pointing to manager object
    Employee employee = new Manager();.
    Now, make a subclass reference point to the super class reference, in our case
    Programmer prog = new Manager().
    This is like saying long y = 10L;int x = y;.So it gives compiler error.
    Just like downcasting primitives, int x = (int)y downcast the Employee reference to Programmer reference.
    Programmer prog = new Manager(); the compiler error is gone.
    Next, lets call the overridden method calcBonus() using programmer reference.
    This line gives ClassCastException. Why?. The reason is the programmer reference is actually pointing to manager object.These two classes are not in herarchy. They are in the same level under Employee. So it throws ClassCastException.

    Let us see the code that causes ClassCastException

    One more example

  19. How will you prevent the class from being inherited/extended?
  20. Use the keyword final to make the class from being extended.
    eg. public final class Employee{}

    The classes String, Math from java.lang package are final classes

  21. What is a final method?
  22. A final method cannot be overridden in the subclass.
    eg. public final void adminDetails(){}

  23. How do you make a variable constant?
  24. Use the keyword final to make the variable constant. In case of final variable, the value should be given during declaration time itself
    eg. public final int BONUS = 2000;

  25. Can an abstract class have both final and abstract methods?
  26. Yes.

  27. Difference between abstract, concrete, final, private methods.
  28. concrete method: This is a normal method with proper definition. It can be overridden and or called directly by the subclasses.
    eg.public void greetMessage(){ }

    abstract method: This is a method with only declaration and no definition. This method has to be implemented in the subclass to provide the functionality.
    eg.public abstract void calcBonus();

    final method: A final method cannot be overridden in the subclass. This method can be called by the subclass.
    eg.public final void adminDetails(){ }

    private method: A private method in the super class is not even visible for the subclass.
    eg.private void salaryDetails(){ }


  29. How abstraction and Encapsulation are complementary to each other?
  30. From Object Oriented Analysis and Design, page 49, second edition:
    Abstraction and encapsulation are complementary concepts. Abstraction focuses on the observable behavior of an object. Encapsulation focuses upon the implementation that gives rise to this behavior. Encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics.

    In simple words,
    Abstraction is a technique that help us identify which specific information should be visible, and which information should be hidden.
    Encapsulation is then the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.This is achieved using access specifiers

    If you have some more questions relevant to abstraction, then do share with us.Also learn about top questions and answers in interfaces.

    Happy Learning

Post a comment