+1 vote
in JAVA by
Java abstraction interview questions in java

1 Answer

0 votes
by

Abstract Class Interview Questions and Answers for best Practice

Question#1. What is Abstraction in Java?

Ans: Abstraction in Java is a technique by which we can hide the data that is not required to users. It hides all unwanted data so that users can work only with the required data.

Question#2. How to achieve or implement Abstraction in Java?

Ans: There are two ways to implement abstraction in java. They are as follows:

a) Abstract class (0 to 100%)

b) Interface (100%)

Question#3. What is Abstract class in Java? How to define it?

Ans: An abstract class in java is a class that is declared with an abstract keyword.

Example of Abstract class:

abstract class Test {

abstract void show();

}

Question#4. What is the difference between abstract class and concrete class?

Ans: There are mainly two differences between an abstract class and concrete class. They are:

a) We cannot create an object of abstract class. Only objects of its non-abstract (or concrete) sub classes can be created.

b) It can have zero or more abstract methods that are not allowed in a non-abstract class (concrete class).

Question#5. What is Abstract in Java?

Ans: Abstract is a non-access modifier in java that is applicable for classes, interfaces, methods, and inner classes.

Question#6. Can abstract modifier applicable for variables?

Ans: No.

Question#7. What is Abstract method in Java?

Ans: A method which is declared with abstract modifier and has no implementation (means no body) is called abstract method in java.

It does not contain any body. It has simply a signature declaration followed by a semicolon. It has the following general form as given below.

Syntax: 

       abstract type MethodName(arguments); // No body

For example:

      abstract void msg(); // No body.

Question#8. Can an abstract method be declared as static?

Ans: No.

Question#9. Can an abstract method be declared with private modifier?

Ans: No, it cannot be private because the abstract method must be implemented in the child class. If we declare it as private, we cannot implement it from outside the class.

Question#10. What is Concrete method in Java?

Ans: A concrete method in Java is a method which has always the body. It is also called a complete method in java.

Question#11. When to use Abstract class in Java?

Ans: An abstract class can be used when we need to share the same method to all non-abstract sub classes with their own specific implementations.

Question#12. When to use Abstract method in Java?

Ans: An abstract method can be used

a) When the same method has to perform different tasks depending on the object calling it.

b) When you need to be overridden in its non-abstract subclasses.

Question#13. Is abstract class a pure abstraction in Java?

Ans: No, It provides 0 to 100% abstraction.

Question#14. Is it possible to create an object of abstract class in Java?

Ans: No. It is not possible but we can create an object of its subclass.

Question#15. Is it possible that an abstract class can have without any abstract method?

Ans: Yes.

Question#16. Can an abstract class have constructor?

Ans: Yes.

Question#17. Is abstract class allow to define private, final, static, and concrete methods?

Ans: Yes.

Question#18. Is it possible to achieve multiple inheritance through abstract class?

Ans: No.

Question#19. Can we make an abstract class without abstract keyword?

Ans: No, a class must be declared with abstract keyword to make an abstract class.

Question#20. Can we define an abstract method inside non-abstract class (concrete class)?

Ans: No, we cannot define an abstract method in non-abstract class.

For example:

class Test {

   abstract void show();

}

The above code will generate a compile-time error.

Question#21. Which among the following code have errors?

a) abstract class A {

     void m1();

   }

b) public class A {

     abstract void m1();

   }

c) abstract public class A {

    abstract void m1();

   }

d) abstract public class A {

     void m1() { }

   }

e) public abstract class A {

    abstract void m1();

    A(){ }

    void m2() { }

    }

f) public abstract class A {

     abstract int x = 100;

     abstract void m1();

     abstract void m2();

    }

g) public abstract class A {

    abstract void m1();

   }

   public class Test {

   public static void main(String[] args) {

    A a = new A();

    }

   }

h) public abstract class A {

    abstract void m1();

    A(){ }

    static void m2() {System.out.println("Hello Java!"); }

    }

    public class B extends A {

     void m1(){

        A.m2(); 

     }

    }

i) public abstract class A {

    abstract void m1();

   private A(){ }

   }

   public class B extends A { }

Ans: a, b, f, g, i.

Question#22. Will the following code compile successfully? If yes, what will be the output of program?

public abstract class A {

  abstract void m1(A a);

}

public class B extends A {

void m1(A a) {

  System.out.println("One");

 }

}

public class C extends B {

void m1(B b) {

 System.out.println("Two");

 super.m1(new B());

 }

}

public class Test {

public static void main(String[] args) {

C c = new C();

 c.m1(new B());

  }

}

Ans: Yes, the above code will be compiled successfully. The output of above program is Two, One.

Question#23. What will happen if we do not override all abstract methods in subclass?

Or, what will happen if we do not provide implementation for all abstract methods in subclass?

Ans: Java compiler will generate compile time error. We will have to override all abstract methods in subclass.

Question#24. What is the difference between Abstraction and Encapsulation?

Ans: Abstraction hides the implementation details from users whereas, encapsulation wraps (binds) data and code into a single unit.

Question#25. Why abstract class has constructor even though you cannot create object?

Ans: We cannot create an object of abstract class but we can create an object of subclass of abstract class. When we create an object of subclass of an abstract class, it calls the constructor of subclass.

This subclass constructor has a super keyword in the first line that calls constructor of an abstract class. Thus, the constructors of an abstract class are used from constructor of its subclass.

If the abstract class doesn’t have constructor, a class that extends that abstract class will not get compiled.

Question#26. Why should we create reference to superclass (abstract class reference)?

Ans: We should create a reference of the superclass to access subclass features because superclass reference allows only to access those features of subclass which have already declared in superclass.

If you create an individual method in subclass, the superclass reference cannot access that method. Thus, any programmer cannot add their own additional features in subclasses other than whatever is given in superclass.

Question#27. What is the advantage of Abstract class in Java?

Ans: The main advantages of using abstract class are as follows:

Abstract class makes programming better and more flexible by giving the scope of implementing abstract methods.

Programmer can implement abstract method to perform different tasks depending on the need.

We can easily manage code.

Question#28. Will the code compile successfully? If yes, what will be the output?

public abstract class A {

abstract void m1();

}

public class B extends A {

 void m1(){

  System.out.println("m1 in class B");

 }

}

public class Test {

public static void main(String[] args) {

B b = new B();

 b.m1();

  }

}

Ans: Yes, the code will be compiled successfully. The output is m1 in class B.

Question#29. Consider the below given code.

public abstract class A {

abstract void m1();

void m2(){

 System.out.println("One”);

 }

}

How to call m2() method in the above code?

Ans: Make it static and call as A.m2();

Question#30. Identify the errors in the following code.

public abstract class A {

abstract void m1();

void m2(){

 System.out.println("One");

 }

}

public class B extends A {

 void m2(){

  System.out.println("Two");

 }

}

public class Test {

public static void main(String[] args) {

B b = new B();

 b.m2();

  }

}

Ans: The abstract method m1() has been not implemented (overridden) in subclass B. Therefore, we will get a compile-time error.

Question#31. Will the given code compile successfully? If yes, how?

a)

public abstract class A {

abstract void m1();

}

public class B extends A {

 private void m1(){

  System.out.println("One");  

 }

}

public class Test {

public static void main(String[] args) {

B b = new B();

 b.m1();

  }

}

Ans: No, the code will not be compiled successfully because we cannot reduce the visibility of the inherited method from A.

b)

public abstract class A {

abstract void m1();

}

public class B extends A {

 protected void m1(){

  System.out.println("One");  

 }

}

public class Test {

public static void main(String[] args) {

B b = new B();

 b.m1();

  }

}

Ans: Yes, the code will be compiled successfully. The output is One.

c)

public abstract class A {

public abstract void m1();

}

public class B extends A {

 protected void m1() {

     System.out.println("One");  

 }

}

public class Test {

public static void main(String[] args) {

B b = new B();

 b.m1();

  }

}

Ans: No, compile-time error because we cannot reduce the visibility of inherited method m1() from A.

d)

public abstract class A {

private abstract void m1();

}

public class B extends A {

 protected void m1(){

  System.out.println("One");  

 }

}

public class Test {

public static void main(String[] args) {

B b = new B();

 b.m1();

  }

}

Ans: Abstract method m1() in class A cannot be private.

e)

public abstract class A {

protected abstract void m1();

}

public class B extends A {

 void m1(){

  System.out.println("One");  

 }

}

public class Test {

public static void main(String[] args) {

B b = new B();

 b.m1();

  }

}

Ans: No, compile time error.

f)

public abstract class A {

protected abstract void m1();

}

public class B extends A {

 protected void m1(){

  System.out.println("One");  

 }

}

public class Test {

public static void main(String[] args) {

B b = new B();

 b.m1();

  }

}

Ans: Yes, the code will be compiled successfully. The output is One.

g)

public abstract class A {

static abstract void m1();

}

public class B extends A {

 void m1(){

  System.out.println("One");  

 }

}

public class Test {

public static void main(String[] args) {

B b = new B();

 b.m1();

  }

}

Ans: No, compile-time error because we cannot set non-access modifiers with abstract method m1() in class A.

Question#32. Will the below code compiled successfully? If yes, what will be the output of the program?

public abstract class A {

abstract void m1();

abstract void m2();

}

public class B extends A {

 void m1(){

  System.out.println("One");  

 }

 void m2(){

  System.out.println("Two");  

 }

 void m3() {

  System.out.println("Three");  

 }

}

public class Test {

public static void main(String[] args) {

A a = new B();

 a.m1();

 a.m2();

 a.m3();

  }

}

Ans: No, compile-time error because we cannot call m3() method defined in class B.

Question#33. Will the below code be compiled successfully? If yes, what will be the output of the following program?

public abstract class A {

abstract void m1();

 A(){ System.out.println("Hello"); }

 static void m2() {System.out.println("Hello Java!"); }

}

public class B extends A {

 void m1(){

  A.m2(); 

 }

}

public class Test {

public static void main(String[] args) {

 B b = new B();

  b.m1();

 }

}

Ans: Yes, Output is Hello, Hello Java!

Question#34. What will be the output of the following program?

public abstract class A {

abstract void m1();

 A(){ System.out.println("Red"); }

 static void m2() {System.out.println("Orange"); }

}

public class B extends A {

 void m1(){

  A.m2();

  System.out.println("Pink");

 }

}

public class C extends B {

 void m3(){

    System.out.println("Green"); 

  }

 }

public class Test {

public static void main(String[] args) {

C c = new C();

 c.m3();

 c.m1();

  }

}

Ans: Output: Red, Green, Orange, Pink.

Question#35. Identify the error in the below code and correct the error.

public abstract class A {

abstract void m1(int x, int y);

}

public class B extends A {

 void m1(){

  System.out.println(" ");

 }

}

Ans: The class B must implement inherited abstract method as void m1(int x, int y) { System.println.out(” “);}

Question#36. Correct the given code and find out the output?

public abstract class A {

abstract void m1(int x, double y);

}

public class B extends A {

 void m1(double y, int x){

  System.out.println("Hello Java!");

 }

}

public class Test {

public static void main(String[] args) {

A a = new B();

 a.m1(20, 20.50);

 }

}

Ans: Correction code is:

public class B extends A {

 void m1(int x, double y){

  System.out.println("Hello Java!");

 }

}

The output is “Hello Java!”.

Question#37. What will be the output of the below program?

public abstract class A {

 private int x;

 A(int x){

  System.out.println("Value of x: " +x);

 }

  abstract void m1(int x, double y);

}

public class B extends A {

 private int y;

 B(int y){

   super(10);

   System.out.println("Value of y: " +y);

}

void m1(int x, double y){

  System.out.println("One");

 }

void m2(){

  System.out.println("Two"); 

  this.m1(5, 10.50);

 }

}

public class C extends B {

 C(){

 super(30);  

 }

 void m1(int x, double y){

   super.m1(10, 15.15);

   System.out.println("Three"); 

  }

}

public class Test {

public static void main(String[] args){

B b = new C();

 b.m1(10, 20.50);

 b.m2();

  }

}

Ans: Output is:

Value of x: 10

Value of y: 30

One

Three

Two

One

Three

Question#38. What will be the output of the below program?

public abstract class A {

 abstract void m1(int x, double y);

 abstract void m2(String name);

}

public class B extends A {

void m1(int x, double y) {

  System.out.println("One");

}

void m2(String name){

  System.out.println("Two");

 }

}

public class C extends B {

static void m1(){

 super.m1(20, 30);

  }

 }

public class Test {

public static void main(String[] args){

 C.m1();

  }

}

Ans: The code cannot be compiled because super keyword cannot be used in static method.

Question#39. What will be the output of the following program?

public abstract class A {

abstract void m1(int x, double y);

abstract void m2(String name);

}

public class B extends A {

void m1(int x, double y){

  System.out.println("One");

}

void m2(String name){

  System.out.println("Two");

}

void m3(){

 System.out.println("Three");

}

void m4(){

 System.out.println("Four");

 }

}

public class C extends A {

 B b;

void m1(int x, double y){

 b = new B();

 b.m3();

}

void m2(String name){

  b = new B();

  b.m4();

  }

}

public class Test {

public static void main(String[] args){

A a = new C();

 a.m1(20, 30.0);

 a.m2("abc");

  }

}

Ans: Output is Three, Four

Question#40. What will be the output of the below program?

public abstract class A {

abstract void m1(A a);

abstract void m2(A a);

}

public class B extends A {

void m1(A a) {

  System.out.println("One");

}

void m2(A a) {

  System.out.println("Two");

 }

}

public class C extends B {

void m1(B b) {

 System.out.println("Three");

 super.m1(new B());

}

void m2(B b) {

 System.out.println("Four");

 super.m2(new B());

  }

 }

public class Test {

public static void main(String[] args){

C c = new C();

 c.m1(new B());

 c.m2(new B());

  }

}

Ans: Output: Three, One, Four, Two.

Java Abstraction Tricky Questions for Interview :

Question#1) Abstract class must have only abstract methods. True or false?

False. Abstract methods can also have concrete methods.

Question#2) Is it compulsory for a class which is declared as abstract to have at least one abstract method?

Not necessarily. Abstract class may or may not have abstract methods.

Question#3) 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 not be abstract.

Question#4) Why final and abstract can not be used at a time?

Because, final and abstract are totally opposite in nature. A final class or method can not be modified further where as abstract class or method must be modified further. “final” keyword is used to denote that a class or method does not need further improvements. “abstract” keyword is used to denote that a class or method needs further improvements.

Question#5) Can we instantiate a class which does not have even a single abstract methods but declared as abstract?

No, We can’t instantiate a class once it is declared as abstract even though it does not have abstract methods.

Question#6) Can we declare abstract methods as private? Justify your answer?

No. Abstract methods can not be private. If abstract methods are allowed to be private, then they will not be inherited to sub class and will not get enhanced.

Question#7) 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. That’s why abstract classes should have constructors. Even if you don’t write constructor for your abstract class, compiler will keep default constructor.

Question#8) Can we declare abstract methods as static?

No, abstract methods can not be static.

Question#9) Can a class contain an abstract class as a member?

Yes, a class can have abstract class as it’s member.

Question#10) Abstract classes can be nested. True or false?

True. Abstract classes can be nested i.e an abstract class can have another abstract class as it’s member.

Question#11) 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.

Question#12) Can we declare local inner class as abstract?

Yes. Local inner class can be abstract.

Question#13) Can abstract method declaration include throws clause?

Yes. Abstract methods can be declared with throws clause.

...