Top Java Abstraction Interview Questions (2024) | TechGeekNext

Top Java Abstraction Interview Questions (2024)

  1. What is Abstraction in Java?
  2. How Abstraction is helpful in java?
  3. What are the advantages of Abstraction?
  4. How Abstraction is achieved in java?
  5. When to use abstract class in Java?
  6. When to use the abstract method in Java?
  7. What are the differences between Abstraction and Encapsulation?
  8. What are the difference between Abstraction and Polymorphism?
  9. What are interfaces in Java?
  10. Difference between Abstract class and Interface in java?
  11. Can abstract method can be defined inside the non-abstract class?
  12. Should abstract methods be overridden?
  13. Is it possible for one interface to extend another?

Q: What is Abstraction in Java?
Ans:

Abstraction is a design concept where unwanted data is hidden from the user and result is derived for useful data. Abstraction is the design concept where only functionality is declared and does not define it. In other words, Abstraction only focuses on the object and not how it is implemented.

Q: How Abstraction is helpful in java?
Ans:

Abstraction is one of the main concepts of OOPS(Object-oriented program). Abstraction is very much useful in Java as it reduces the complexity and efforts of programming from selecting Data from the big pool and showing results which is important for the users.

Abstraction only focuses on the object and not on its implementation. In simple words, Abstraction is a method of hiding certain details from end-users and only showing essential information which is necessary for users.

Q:What are the advantages of Abstraction?
Ans:

The advantages of Abstraction are:

  • It is helpful in reducing the complexity of data.
  • It derives the result for a specific class.
  • It is helpful in hiding unwanted details from users.
  • The best example of Abstraction is Television. To use the Television, we do not need to understand how it works internally.
Take a look at our Suggested Posts :

Q: How Abstraction is achieved in java?
Ans:

Abstraction can be achieved by declaring a specific keyword abstract. The keyword abstract is used as a non-access modifier with the class and method.

Abstraction can be accomplished through the use of abstract classes or interfaces. Abstraction can be used with methods and classes.

  1. abstract with class:
  2. abstract with the method:
Let understand in details :
  1. abstract with class

    Abstraction class means when the word abstract is used with class, then no one can instantiate that class. The classes and the methods of implication can be extended.

      Important points to remember:
    • An abstract class is always declared with the keyword 'abstract'.
    • It can have both abstract, non-abstract methods.
    • It is non-instantiable.
    • It must have static and concrete methods.
    • The sub-class body and the method cannot be changed.
    Example of abstract class:
    abstract class RBI{
    abstract int getRateOfInterest();
    }
    class CBN extends RBI{
    int getRateOfInterest(){return 6;}
    }
    class KNB extends RBI{
       int getRateOfInterest(){return 9;}
    }
    
    class TestRBI{
    public static void main(String args[]){
    RBI b;
    b=new CBN();
    System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
    b=new KNB();
    System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
    }}
    
    //Output
    Rate of Interest is: 6 %
    Rate of Interest is: 9     
  2. abstract with the method

    Abstraction with the method means when the word abstract is used with the method where the overridden is must for the first concrete class. The implementation of this method is done where there is only an abstract class and the subclass which is provided that is inherited.
      Important points to remember:
    • It must have a concrete class.
    • It does not have any implementation with the body.
    • It is used when the class is abstracted.
    • The implementation is achieved when the sub-class is inherited.
    Example of abstract method:
    abstract class Bird {
      // Abstract method does not have a body
      public abstract void birdSound();
      // Common actions
      public void sleep() {
        System.out.println("Azzz");
      }
    }
    
    // Subclass
    class Parrot extends Bird {
      public void birdSound() {
        // The implementation of birdSound() 
        System.out.println("Theparrotsays: bzzz bzzz");
      }
    }
    
    class Main {
      public static void main(String[] args) {
        Parrot myParrot = new Parrot();
        myParrot.birdSound();
        myParrot.sleep();
      }
    }
    
    ------------------------------------------
    Output:
    The Parrot says Azzz, Azzz,
    
    bzzz.
    
    

Q:When to use abstract class in Java?
Ans:

The abstract class is used when the class is defined by the keyword 'abstract'.
Following are the ways to use the abstract class:

  • The class must be written/assigned as an abstract class.
  • The hierarchy is maintained by using abstract class.
  • In abstract class types of behavior and implementation details can be known.
  • abstract class class_name {
        //abstract or non-abstract methods   
    }   

Q:When to use the abstract method in Java?
Ans:

Following are the ways to use the abstract method:

  • The abstract method is used when the word 'abstract' is used in abstract class.
  • The abstract method is used when the class is concrete.
  • It declares two more sub-classes.
  • The classes must be Overridden.
  • abstract return_type method_name( [ argument-list ] );  

Q: What are the differences between Abstraction and Encapsulation?
Ans:

Difference
Abstraction
Encapsulation

Basic

Abstraction means hiding details from the users and showing only necessary details to the users.

Encapsulation means binding data in a single unit.

Focus

Abstraction solves the problem at the design level.
Abstraction only focuses on the object and not how it works.

Encapsulation solves the problem at the implementation level.

Encapsulation focus on an binding data in one unit and implementing its object, its behavior and properties, and what it does.

Conclusion

Abstraction breaks the complexity of the data.

Encapsulation maintains the flexibility of the data.

Q: What are the difference between Abstraction and Polymorphism?
Ans:

Difference
Abstraction
Polymorphism

Basic

Abstraction is a technique of showing necessary details.

Polymorphism is a process for implementing inherited data.

Focus

Abstraction is based on design level pattern. There are two types of abstraction:

  • Data Abstraction
  • Process Abstraction

Polymorphism is used when single or many types are not defined by name it represents any type by using abstract symbols.

There are two types of Polymorphism:

  • Compile-time Polymorphism (or Static polymorphism) - Example Method Overloading
  • Runtime Polymorphism (or Dynamic polymorphism) - Example Method Overriding

Conclusion

Abstraction can be implemented , when the main class is static or concrete. Overridden is must.

Polymorphism can be implemented when data is static or dynamic.

Q: What are interfaces in Java?
Ans:

The interface is part of an abstract class. The object class cannot be created by using an interface. The privacy/security is maintained by using an interface. The multiple inheritances of the sub-classes is implemented by using the interface.

Q: Difference between Abstract class and Interface in java?
Ans:

Abstract class
Interface

In abstract class both the methods are used abstract and non-abstract.

In interface, only an abstract method is used.

Multiple inheritances are not supported in abstract classes.

The interface supports multiple inheritances.

The implementation of an interface is provided by the abstract class.

The interface cannot provide implementation to an abstract class.

The abstract is protected and private.

An interface is only public.

Q: What are the difference between Abstraction and Polymorphism?
Ans:

Abstract class
Interface

In abstract class both the methods are used abstract and non-abstract.

In interface, only an abstract method is used.

Multiple inheritances are not supported in abstract classes.

The interface supports multiple inheritances.

The implementation of an interface is provided by the abstract class.

The interface cannot provide implementation to an abstract class.

The abstract is protected and private.

An interface is only public.

Q: Can abstract method can be defined inside the non-abstract class?
Ans:

No. An abstract method cannot be defined inside the non-abstract class.

class Test {
   abstract void show();
}
Output: Above program will throw a compile-time error.

Q: Should abstract methods be overridden?
Ans:

Yes, all the abstract methods in the subclass should be overridden because the java compiler will not accept and give the compile-time error.

Q: Is it possible for one interface to extend another?
Ans:

In Java, the interface can be extended by another interface.

// this interface extends from the Spider interface:
public interface SixLegs extends Spider
{
    public void walkWithSixLegs( );
...

Recommendation for Top Popular Post :