Superclass in java Interview MCQ Questions and Answers
Table of Contents
Study and learn Interview MCQ Questions and Answers on Superclass in java . You can know about Multilevel and Multiple Inheritances. Attend job interviews easily with these Multiple Choice Questions
Inheritance Superclass in java & Subclass in java Examples Tutorial
As Java is an Object Oriented Programming (OOP) language, it supports Inheritance. Inheritance is a process of acquiring Methods and Properties of an existing class into a custom class written by us so that all the inherited code becomes part of our custom Class. Here, the already existing Class is called a Superclass and the custom Class written by us is called a Subclass. Let us know more about Java Inheritance with Superclass and Subclass relationship and usage with examples in this Java Tutorial.
Superclass and Subclass Examples
Let us take Television or TV as our Superclass which is already existing with some methods and properties. Properties are simply variables.
Java allows inheritance only through the “extends” keyword. The class being inherited is called a Superclass and the class inheriting the Superclass is called a Subclass.
Syntax:
class Subclass extends Superclass { //some code }
Superclass in java
We explain Superclass-Subclass relationship using a TV example. As the technology evolved, more and more features were added to the basic Superclass TV. There came an LCD TV with Good Audio and Video. Then came LED TV with even good Video and true Dolby Audio. Additions like Smart TV with an Android operating system on top of it made the Basic TV like a modern computer. TVs now support Bluetooth, Wifi, Ethernet and HDMI input outputs
All Subclasses have access to the properties and methods of the Superclasses depending on the access level defined by an access modifier like public, protected or private.
A Subclass can become a Superclass to another class down the hierarchy. The chain continues up to any level with the last class being a Subclass to the Class one level up.
At any point in time, a Class can inherit only one Class through extends keyword.
Let us define our example superclass CRT_TV. It defines two properties or variables audioChannels and resolution.
class CRT_TV { char category='A'; int audioChannels = 1; int resolution = 640*480; //480p void showVideo(int res) { System.out.println("Showing Video Pixels " + res); } }
Now, we shall extend or inherit the class CRT_TV from LCD_TV class. CRT_TV becomes a Superclass to the LCD_TV Subclass.
class LCD_TV extends CRT_TV { int audioChannels = 2; int resolution = 1280*720; //720p void connectBluetooth() { System.out.println("Connecting Bluetooth"); } void show2() { //super keyword to avoid name clash System.out.println("Access Super Resolution= " + super.resolution); } }
LCD_TV subclass defined an extra feature using “connectBluetooth” method. It also improved existing features like audioChannels and resolution of the Superclass CRT_TV. Subclass LCD_TV has access to the Superclass’s method “showVideo” and variables audioChannels, resolution and category. Notice that we have overwritten (overridden) the two properties “audioChannels” and “resolution“. Yes, Java inheritance allows customizing the inherited methods too.
Let us use the above classes into a testing class “TestingInheritance”.
class TestingInheritance { public static void main(String[] args) { CRT_TV crt = new CRT_TV(); crt.showVideo(crt.resolution); LCD_TV lcd = new LCD_TV(); System.out.println("Accessing Superclass category= " + lcd.category); lcd.showVideo(lcd.resolution); //accessing superclass method lcd.show2(); //try to know superclass resolution lcd.connectBluetooth(); //crt.connectBluetooth(); //error: can not access Subclass's methods with a superclass reference. System.out.println("LCD Audio Channels= " + lcd.audioChannels); System.out.println("CRT Audio Channels= " + crt.audioChannels); } } //OUTPUT Showing Video Pixels 307200 Accessing Superclass category= A Showing Video Pixels 921600 Access Super Resolution= 307200 Connecting Bluetooth LCD Audio Channels= 2 CRT Audio Channels= 1
Notice that we have accessed a variable “resolution” which is present in both the Superclass and Subclass. You can directly access the own class’s variables. In Java, to access a Superclass’s variable, use the keyword “super” to avoid a name clash with the current instance variable. If the names or identifiers are different, use the variables directly.
We shall learn more about Overriding Methods in the coming chapters using the same Java Inheritance concepts explained above.
[WpProQuiz 164]
Superclass in java Interview MCQ
1) What are the features of an Object Oriented Programming (OOPs)?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) All the above
Answer [=] D
2) What are the features reused using Inheritance in Java?
A) Methods
B) Variables
C) Constants
D) All the above
Answer [=] D
Explanation:
Variables and Methods are reused through inheritance. Constants are nothing but variables only if they hold some value.
3) The class that is being inherited or subclassed is called ___.
A) Subclass
B) Superclass
Answer [=] B
Explanation:
Superclass or Super-Class
4) The class that inherits an already defined class is called ___.
A) Subclass
B) Superclass
Answer [=] A
Explanation:
Subclass
5) Java language supports ___ type of inheritance.
A) Multiple Inheritance
B) Multi-Level Inheritance
Answer [=] B
Explanation:
Multi-Level Inheritance is somewhat complicated.
6) You should use Inheritance when there is an IS-A relationship between classes. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
Yes. One simple example is ANIMAL Superclass and HORSE Subclass. HORSE is-a/is-an ANIMAL.
7) What are the types of Inheritances (Whether Java supports or not) available in Object-Oriented Programming Languages?
A) Single Inheritance
B) Multi-Level Inheritance, Hierarchical Inheritance
C) Multiple Inheritance, Hybrid Inheritance
D) All the above
Answer [=] D
Explanation:
Java supports extending from only one Superclass. Multilevel inheritance is completely supported by Java. Whereas Multiple and Hybrid inheritances are based on Multiple-Superlclasses scenario and hence not supported by Java.
8) In a Single inheritance, Class B inherits only from Class A. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
True.
9) In a Multi Level Inheritance Class-C inherits from Class-B and Class-B inherits from Class-A. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
True
10) In a Multi-Level Inheritance in Java, the last subclass inherits methods and properties of ____.
A) Only one immediate Superclass
B) Few classes above it.
C) All classes above it
D) None
Answer [=] C
Explanation:
Yes. The last class inherits all of the properties and methods of all of the classes above it in the chain.
11) When a Class inherits two superclasses (not in Java), it is called ____ inheritance.
A) Multilevel inheritance
B) Single Inheritance
C) Multiple Inheritance
D) None
Answer [=] C
Explanation:
Multiple Inheritance
12) A Subclass can become a Superclass to another class extending from it in Java. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
Yes, true.
13) You can not inherit a Superclasses constructor even after using inheritance in Java. State TRUE or FALSE.
A) TRUE
B) FALSE
Answer [=] A
Explanation:
Yes. True. A Constructor is class-specific. It is not like a method that can be shared.
14) Find Superclass and Subclass in the below Java code snippet?
class B { void show(){} } class A { void hide(){} }
A) B is superclass and A is subclass.
B) A is superclass and B is a subclass.
C) There is no superclass or subclass present.
D) None
Answer [=] C
Explanation:
As there is no use of the “extends” keyword, inheritance does not come into the picture. So, there is no superclass or subclass present in the above example.
15) Find Superclass and Subclass in the below Java program?
class Liquid { void pour(){} } class Juice extends Liquid { void filter(){} }
A) The Liquid is a superclass and Juice is a subclass.
B) The Liquid is a Subclass and Juice is a Superclass.
C) There is no superclass or subclass
D) None
Answer [=] A
Explanation:
As the Juice class is extending from the Liquid class, Juice is a subclass and Liquid is a superclass. Simply put, the class before the “extends” keyword is always a Subclass.
16) Which is the keyword used to implement inheritance in Java?
A) extends
B) implements
C) instanceof
D) None
Answer [=] A
Explanation:
The keyword “extends” tells the compiler that the class on the left side is subclassing the class on the right side.
17) What is the output of the below Java program with inheritance?
class Sweet { void price() { System.out.print("Sweet=$10 "); } } class Sugar extends Sweet { void price() { super.price(); System.out.print("Sugar=$20"); } } public class JavaInheritance1 { public static void main(String[] args) { Sugar su = new Sugar(); su.price(); } }
A) Sweet=$10 Sugar=$20
B) Sweet=$10 Sugar=$10
C) Sweet=$20 Sugar=$20
D) Compiler error
Answer [=] A
Explanation:
Notice the use of the keyword “super”. Using this keyword, you can call superclass’s methods and variables.
18) Can you call it a full-fledged inheritance of using ABSTRACT classes and INTERFACES in Java?
A) NO
B) YES
Answer [=] A
Explanation:
No. Abstract classes and Interfaces do not define a class completely. So, it is not called a full-fledged inheritance of using those.
19) To control inheritance to different classes and levels, Java provides ____.
A) Return types like the void, int, float, double and other object types
B) Static keyword
C) Access modifiers like default, public, protected, private
D) None
Answer [=] C
Explanation:
Access modifiers like default (not a keyword), public, protected and private changed the visibility of a method, variable or a class. Even the keyword “package” also allows grouping of classes and control inheritance levels to some extent.
20) To stop or block inheriting a given class, the ___ keyword is used before the class.
A) static
B) private
C) final
D) none of the above
Answer [=] C
Explanation:
You can not subclass a class that is marked FINAL.
final class CLASS_NAME //Can not subclass { } class SUBCLASS extends CLASS_NAME //ERROR { }
For More Free online Test of previous years Question Papers of Aptitude Click here
For More Free online Quizzes of JAVA PROGRAMMING Test online Click here