Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Inheritance in Java , Superclass Subclass Examples Tutorial

Inheritance Superclass Subclass

Table of Contents

    • What is Inheritance
  • Inheritance in Java with Superclass and Subclass Examples
    • Inheritance meaning
    • Let us define our example superclass CRT_TV. It defines two properties or variables audioChannels and resolution.
  • Inheritance in Java Interview MCQ
    • 1) What is the maximum number of levels possible in a Multilevel Inheritance in Java?
    • Inheritance in Java

Inheritance in Java Superclass Subclass Questions and Answers Part2

Study and learn Interview MCQ Questions and Answers on Inheritance in Java Superclass Subclass . You can know about Multilevel and Multiple Inheritances. Attend job interviews easily with these Multiple Choice Questions

What is Inheritance

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.

Inheritance in Java with 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
}

Inheritance meaning

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 165]

Inheritance in Java Interview MCQ

1) What is the maximum number of levels possible in a Multilevel Inheritance in Java?

A) 8

B) 16

C) 32

D) No maximum level

Answer [=] D

Explanation:

There is no limit to the number of levels in a multilevel inheritance chain in Java.

2) What is the output of the below java program with Constructors and Inheritance?

class Processor
{
  Processor()
  {
    System.out.print("Inside Processor() Constructor. ");
  }
}
class I3Processor extends Processor
{
  I3Processor()
  {
    System.out.print("Inside I3Processor() Constructor. ");
  }
}
class I5Processor extends I3Processor
{
  I5Processor()
  {
    System.out.print("Inside I5Processor() Constructor. ");
  }
}

public class JavaInheritance2
{
  public static void main(String[] args)
  {
    I5Processor i5 = new I5Processor();
  }
}

A) Inside I5Processor() Constructor. Inside I3Processor() Constructor. Inside Processor() Constructor. 

B) Inside I5Processor() Constructor. Inside I5Processor() Constructor. Inside I5Processor() Constructor. 

C) Inside Processor() Constructor. Inside I3Processor() Constructor. Inside I5Processor() Constructor. 

D) Compiler error

Answer [=] C

Explanation:

The example demonstrates the Invoking of constructors using a Multilevel Inheritance. Always, the default superclass’s constructor is invoked. And then, the subclass’s constructor is invoked.

Processor <-- I3Processor <-- I5Processor

3) What is the output of the below Java program with Constructors using Inheritance?

class Ant
{
  Ant(String name)
  {
    System.out.print("Inside Ant(String) Constructor. ");
  }
}
class WildAnt extends Ant
{
  WildAnt()
  {
    System.out.print("Inside WildAnt() Constructor. ");
  }
}

public class Inheritance3
{
  public static void main(String[] args)
  {
    WildAnt wa = new WildAnt();
  }
}

A) Inside WildAnt() Constructor.

B) Inside Ant(String) Constructor. Inside WildAnt() Constructor.

C) Inside WildAnt() Constructor. Inside WildAnt() Constructor. 

D) Compiler error

Answer [=] D

Explanation:

Compiler throws an error “implicit constructor of Ant class is undefined”. Once you define a Constructor with some arguments, the compiler does not add a default constructor with zero arguments. To subclass a class, defining a default constructor is mandatory.

4) If a class is not subclassed by any class, then defining a default constructor is not mandatory in Java. State TRUE or FALSE.

A) TRUE

B) FALSE

Answer [=] A

Explanation:

True.

5) What is the output of the below Java program?

final class Bus
{
  void show()
  {
    System.out.print("Generic Bus. ");
  }
}
class ElectricBus extends Bus
{
  void show()
  {
    System.out.println("Electric Bus. ");
  }
}
public class Inheritance4
{
  public static void main(String[] args)
  {
    ElectricBus eb = new ElectricBus();
    eb.show();
  }
}

A) Generic Bus

B) Electric Bus

C) Generic Bus. Electric Bus.

D) Compiler error.

Answer [=] D

Explanation:

Notice the use of the keyword “final” before the superclass BUS. You can not subclass a class that is marked final.

6) A Superclass reference can refer to a Subclass Object without casting. State TRUE or FALSE.

A) TRUE

B) FALSE

Answer [=] A

Explanation:

Yes.

7) A superclass reference can not be used to invoke a method or variable of the subclass. State TRUE or FALSE.

A) TRUE

B) FALSE

Answer [=] A

Explanation:

Yes. A superclass reference knows only about the methods and properties of the same class but not the subclass.

8) A subclass object can be used to invoke (call) a method or property of the superclass. State TRUE or FALSE.

A) TRUE

B) FALSE

Answer [=] A

Explanation:

Yes, True.

9) What is the output of the below Java program on the references of Superclass and Subclass?

class Food
{
  void show()
  {
    System.out.print("FOOD ");
  }
}
class Bread extends Food
{
  void toast()
  {
    System.out.print("TOASTED ");
  }
}
public class Inheritance5
{
  public static void main(String[] args)
  {
    Food foo = new Food();
    foo.show();
    Food foo2 = new Bread();
    foo2.show();
    Bread br = new Bread();
    br.toast();
    br.show();
  }
}

A) FOOD FOOD FOOD FOOD 

B) FOOD FOOD TOASTED FOOD 

C) FOOD TOASTED FOOD FOOD 

D) Compiler error

Answer [=] B

Explanation:

You can only invoke methods of the Superclass using a Superclass reference variable pointing to a Subclass object.

10) What is the output of the below Java program using Inheritance?

class Furniture
{
  void show()
  {
    System.out.println("Made of Wood. ");
  }
}
class Sofa extends Furniture
{
  void addCushion()
  {
    System.out.println("Added. ");
  }
}
public class Inheritance6
{
  public static void main(String[] args)
  {
    Furniture fur = new Sofa();
    fur.addCushion();
  }
}

A) Added.

B) No output

C) Added. Made of Wood.

D) Compiler error

Answer [=] D

Explanation:

Yes. The compiler throws an error saying “The method addCushion() is undefined for the type Furniture”. It means that you can not call a method of subclass using a superclass reference even though it is pointing to a subclass object.

Inheritance in Java


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

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.