Abstract Class in java Interview MCQ Questions and Answers
Table of Contents
Study and learn JAVA Programming test online Questions and Answers on Abstract Class in Java. Attend job interviews easily with these Tutorial And Multiple Choice Questions
what is abstract class in java
Abstract Class in java
A Java Abstract Class is nothing but a class with one or more Unimplemented methods. These Incomplete methods should Implemented by the first Concrete subclass of an abstract class. Let us know more about Abstract Classes in this Java Tutorial.
Note: A Concrete class is any Java class that defines all methods for Readymade use. Objects of a Concrete class can created using the keyword “new“.
Abstract Class in java Example
You can not Instantiate an Abstract Class to create an object. It is not a normal fully working class with methods and Variables. Some Implementation is left to the subclass. So, it is up to the Developer or Implementor to complete the method body of an Abstract method of an abstract class.
Abstract Class Syntax:
abstract class CLASS_NAME { abstract return-type METHOD_NAME(PARAMETERS); //Ending with a Semicolon }
To create Abstract Class, you should use the keyword “abstract” before the keyword “class“. The keyword “abstract” should precede before each Abstract method of an Abstract Class. You should end the abstract method with a semicolon (;) symbol just after the closing parentheses “)” without any method-body.
Note: Abstract class can only subclassed or inherited using the keyword “extends“. Abstract methods will converted into concrete methods by a Subclass through inheritance by maintaining the same method signature. You can not say that subclass is overriding an abstract method as the superclass is an abstract class and it does not support the creation of objects in memory.
Check the below example once. Abstract class can contain concrete methods like seating(), ambience() and music() which can used directly after subclassing the abstract class.
Abstract Class in java Example 1
abstract class DiningHall { abstract void food(); void ambience() { System.out.println("Ambience Ready"); } void seating() { System.out.println("Seating Ready"); } void music() { System.out.println("Music YES"); } } class DiningHallSubclass extends DiningHall { void food() { //Writing code is optional. No compiler error. System.out.println("Serve Pizza and Burger.."); } } public class AbstractClassExample1 { public static void main(String[] args) { //Superclass reference DiningHall dh = new DiningHallSubclass(); dh.food(); dh.ambience(); //Same class reference DiningHallSubclass dhs = new DiningHallSubclass(); dhs.food(); dhs.music(); } } //OUTPUT Serve Pizza and Burger.. Ambience Ready Serve Pizza and Burger.. Music YES
In the above example, DiningHall is an abstract class with an abstract method food(). So the DiningHallSubclass has to define its own version of food() method after inheriting the DiningHall class. The constructor of the abstract class is invoked by the default constructor of the implementing subclass.
Abstract Class creation and usage Rules:
Find all the rules to create a compiler friendly Java Abstract Class below. Also, know how to use an Abstract Class.
- Abstract class need not contain any abstract methods.
- An Abstract class with 100% concrete methods compiles and runs without errors or exceptions.
- And Abstract class or abstract method can created only by using the keyword “abstract“.
- Abstract class can contain variables of type primitives and Objects.
- and Abstract class can contain constants defined using the keyword “final“.
- Abstract class can contain static variables and methods.
- And Abstract methods can not declared with the keyword static.
- Abstract method can throw exceptions. The subclass implementing it need not throw any exception. Calling these methods with a subclass reference will not result in compile errors.
- Abstract class can define a constructor. It allows overloading of constructors also.
- If one method is abstract, the whole class enclosing it should marked abstract.
- Abstract class can not declared “final” as it stops subclassing and implementing.
- Abstract class can inherit or subclass another abstract class using the “extends” keyword.
Look at another example using an Abstract class Juicer. The subclass ABCJuicer implements the abstract method makeJuice().
Abstract Class in java Example 2
abstract class Juicer { abstract void makeJuice(); Juicer() //constructor { System.out.println("JUICER Constructor."); } void addSugar() { System.out.println("1 Tablespoon Sugar added."); } } class ABCJuicer extends Juicer { void makeJuice() { //Coldpress juice //or Normal juice System.out.println("Coldpressed Juice is ready."); } } public class AbstractClassExample2 { public static void main(String[] args) { ABCJuicer juicer = new ABCJuicer(); juicer.makeJuice(); juicer.addSugar(); } } //OUTPUT JUICER Constructor. Coldpressed Juice is ready. 1 Tablespoon Sugar added.
It is the best practice.
[WpProQuiz 167]
Abstract Class MCQ (Important)
1) An abstract class in Java can be created using the keyword ____.
A) final
B) interface
C) abstract
D) static
Answer [=] C
Explanation:
You should precede the keyword “abstract” before the keyword “class”.
2) To create an Abstract class, the keyword “class” is also required. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
True. You create a class using the keyword CLASS and make it abstract using the keyword ABSTRACT.
abstract class ClassA{ }
3) Can you create an object from an abstract class in Java?
A) Yes
B) No
Answer [=] B
Explanation:
No. You can not instantiate or create an object from an abstract class.
4) Which is the opposite of an Abstract Class?
A) Interface
B) concrete class
Answer [=] B
Explanation:
The Concrete Class is the opposite of an Abstract Class. Concrete class defines all the methods completely as opposed to abstract classes that leave implementation to the developers.
5) Choose a correct implementation of an Abstract class in the below Java code?
A) abstract class ClassA { }
B)
abstract class ClassB { abstract void method(); }
C)
abstract class ClassC { void method() { System.out.print("Hello Abstract Class"); } }
D) All the above
Answer [=] D
Explanation:
All the above. An abstract class need not contain a single abstract method also.
6) An abstract class in Java usually contains one or more abstract ____.
A) constructors
B) methods
C) variables
D) None
Answer [=] B
Explanation:
Developers usually write abstract classes with one or more abstract methods to be implemented after inheriting the class.
7) Does the below Java code with abstract method compile?
class Puppy { abstract void showName(); }
A) NO
B) YES
C) –
D) –
Answer [=] A
Explanation:
No. An abstract method can be defined only by an abstract class.
8) What is the output of the below Java program with an abstract class?
abstract class Coffee { Coffee() { System.out.println("Inside Constructor of Coffee.."); } } class ColdCoffee extends Coffee { ColdCoffee() { System.out.println("Inside Constructor of ColdCoffee.."); } } public class AbstractClassTesting { public static void main(String[] args) { ColdCoffee cf = new ColdCoffee(); } }
A) Compiler error
B) Inside Constructor of Coffee..
Inside Constructor of ColdCoffee..
C) Inside Constructor of ColdCoffee..
D) Inside Constructor of Coffee..
Answer [=] B
Explanation:
An abstract class can contain constructors. The order of invoking of constructors is from the Superclass to Subclass.
9) What is the output of the below Java program with an abstract class?
final abstract class Bell { } class DoorBell extends Bell { DoorBell() { System.out.println("DoorBell ringing.."); } } public class AbstractClassTesting2 { public static void main(String[] args) { Bell bell = new DoorBell(); } }
A) DoorBell ringing..
B) No output
C) Compiler error
D) None of the above
Answer [=] C
Explanation:
You can not use the keyword FINAL before an abstract class. The classes declared with the “final” keyword can not be subclassed or inherited. So, the FINAL keyword is banned for use with an abstract class. So, the compiler throws an error.
10) What is the output of the below Java program with an abstract class?
abstract class MathLibrary { static final float PI = 3.1415f; } public class AbstractClassTesting3 { public static void main(String[] args) { System.out.println(MathLibrary.PI); } }
A) No output
B) Compiler error
C) 3.1415
D) None of the above
Answer [=] C
Explanation:
The output will be 3.1415. You can define and use a static final variable inside an abstract class. A static final variable is nothing but a constant. It is common to access a static variable with a DOT operator preceded by the Class Name.
11) What is the output of the below Java program with multiple abstract classes?
abstract class Editor { abstract void show(); } abstract class Author extends Editor { abstract void print(); } class Office extends Author { void show() { System.out.println("Editor method"); } void print() { System.out.println("Author method"); } } public class AbstractClassTesting4 { public static void main(String[] args) { Editor ed = new Office(); ed.show(); Author au = new Office(); au.print(); } }
A)
Editor method Editor method
B)
Author method Author method
C)
Editor method Author method
D) Compiler error
Answer [=] C
12) An object of Multi-level Inherited abstract class can not be created in memory? State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
True. You can not create objects out of an abstract class whether it is single level or Multilevel.
13) An abstract class with 100% abstract methods is equivalent to ______.
A) Concrete class
B) Virtual Class
C) Interface
D) All the above
Answer [=] C
Explanation:
An interface contains only abstract methods. So it is comparable to a 100% abstract class.
14) What is the output of the below Java program with an abstract class?
public abstract class AbstractClassTest5 { public static void main(String[] args) { System.out.println("Inside Main() method.."); } }
A) No output
B) Compiler error
C) Inside Main() method..
D) None of the above
Answer [=] C
Explanation:
It is perfectly allowed to define a public abstract class as long as you do not create objects of it. Remember that the name of the JAVA file is the name of the public class.
15) What is the output of the below Java code with an abstract class and inner class?
public abstract class AbstractClassTest6 { class Anonymous { int a=5; } public static void main(String args[]) { System.out.print("Inner class is present.."); } }
A) Compiler error
B) Inner class is present..
C) No output
D) None of the above
Answer [=] B
Explanation:
Inner classes are allowed to be written inside an abstract class. So, the program compiles. But to use that inner class, subclassing is necessary.
16) Choose a correct statement about Abstract classes?
A) An abstract class can extend a concrete class
B) An abstract class can extend another abstract class
C) An abstract class can Implement any number of Interfaces.
D) All the above.
Answer [=] D
Explanation:
Yes. Abstract classes can extend other classes.
17) Choose correct statements about an Abstract class in Java?
A) An abstract class Implementing an Interface, need not Implement methods of an Interface
B) An abstract class extending another abstract class, need not define methods of the super abstract class.
C) The first Subclass of an abstract class should define all the abstract methods Inherited from all the Interfaces and super abstract classes.
D) All the above
Answer [=] D
18) Just like an Interface, you can define constants in abstract classes in Java. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
TRUE.
19) Abstract classes support ____ Inheritance.
A) Multiple
B) Multilevel
C) –
Answer [=] B
Explanation:
Just like a Cncrete/normal class in Java, abstract classes support Multilevel Inheritance only. You can extend only from one super class.
20) An abstract class can define ____ inside of it.
A) Inner abstract class
B) Inner concrete class
C) static methods
D) All the above
Answer [=] D
For More Free online Quizzes of JAVA PROGRAMMING Test online Click here
For More Free online Test of previous years Question Papers of Aptitude Click here