Java Inheritance

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which keyword is used in Java to establish an inheritance relationship between two classes?

  • derives
  • extends (correct)
  • implements
  • inherits

In the context of Java inheritance, what term is used to refer to the class that is being inherited from?

  • Derived class
  • Child class
  • Subclass
  • Superclass (correct)

What is the primary benefit of using inheritance in Java?

  • Code reusability and method overriding (correct)
  • Improved data encapsulation
  • Increased program execution speed
  • Reduced memory consumption

Which type of inheritance is NOT directly supported in Java for classes, but can be achieved through interfaces?

<p>Multiple inheritance (C)</p> Signup and view all the answers

In Java, what is the purpose of the super keyword?

<p>To call a method of the superclass (D)</p> Signup and view all the answers

Consider a scenario where class B extends class A, and class C extends class B. What type of inheritance does this represent?

<p>Multilevel inheritance (B)</p> Signup and view all the answers

Which of the following best describes hierarchical inheritance in Java?

<p>Multiple classes inheriting from one class. (A)</p> Signup and view all the answers

What is the output of the following Java code?

class Parent {
    void display() {
        System.out.println("Parent");
    }
}

class Child extends Parent {
    void display() {
        System.out.println("Child");
    }
}

public class Test {
    public static void main(String[] args) {
        Parent p = new Child();
        p.display();
    }
}

<p>Child (D)</p> Signup and view all the answers

What is the output of the following Java code snippet?

class A {
    void methodA() { System.out.println("A"); }
}

class B extends A {
    void methodA() { System.out.println("B"); }
    void methodB() { System.out.println("B"); }
}

public class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.methodA();
    }
}

<p>B (D)</p> Signup and view all the answers

What is the output of the following Java code?

class Parent {
    void display() {
        System.out.println("Parent display");
    }
}

class Child extends Parent {
    void display() {
        super.display();
        System.out.println("Child display");
    }
}

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.display();
    }
}

<p>Parent display\nChild display (C)</p> Signup and view all the answers

Consider the classes Animal, Mammal, and Dog where Mammal extends Animal and Dog extends Mammal. If the Animal class has a protected method makeSound(), which classes can directly access makeSound()?

<p>The Animal, Mammal, and Dog classes (D)</p> Signup and view all the answers

A class Vehicle has a method startEngine(). A class Car extends Vehicle and overrides the startEngine() method to include additional car-specific steps. How can Car's startEngine() method call the original startEngine() method defined in Vehicle?

<p>By using the <code>super.startEngine()</code> method (C)</p> Signup and view all the answers

Class X has a private field value. Class Y extends X. Can class Y directly access or modify the value field?

<p>No, because private fields are not inherited (C)</p> Signup and view all the answers

Given a class Animal with a protected method eat(), and a class Dog extending Animal in a different package, which of the following is true?

<p><code>Dog</code> can always access <code>eat()</code> because it's a subclass (B)</p> Signup and view all the answers

Consider two classes, Parent and Child, where Child extends Parent. Both classes have a method named calculate. If you create an instance of Child and call calculate, which version of the method is executed? Assume calculate is not overridden in Child.

<p>The <code>calculate</code> method in <code>Parent</code> is executed. (C)</p> Signup and view all the answers

Which of the following is a valid example of single inheritance in Java?

<p>class A extends B {} (B)</p> Signup and view all the answers

If a subclass defines a method with the same signature (name and parameters) as a method in its superclass, what is this process called?

<p>Method overriding (C)</p> Signup and view all the answers

In a class hierarchy, if a superclass method throws a checked exception, what must the overriding method in the subclass do?

<p>It must throw the same exception or a subclass of it, or not throw any exception at all. (A)</p> Signup and view all the answers

Which of the following statements is true regarding constructors and inheritance in Java?

<p>A subclass can only call the superclass's constructor using the <code>super()</code> keyword as the first statement in its own constructor. (C)</p> Signup and view all the answers

Consider the following code:

class Vehicle {
    public String modelName = "Generic Vehicle";
}

class Car extends Vehicle {
    private String modelName = "Specific Car";

    public void printModelName() {
        System.out.println(modelName);
        System.out.println(super.modelName);
    }

    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.printModelName();
    }
}

What will be the output when the main method is executed?

<p>Specific Car\nGeneric Vehicle (B)</p> Signup and view all the answers

Flashcards

What is Inheritance?

A feature where a class inherits properties/behaviors from another class, promoting code reusability and method overriding.

What does 'extends' do?

Keyword used in Java to establish an inheritance relationship between a subclass and a superclass.

What is a Superclass?

The class whose properties and behaviors are inherited by another class (the subclass).

What is a Subclass?

The class that inherits properties and behaviors from another class (the superclass).

Signup and view all the flashcards

What is the 'super' keyword?

Keyword to refer to the superclass, useful for calling superclass methods or accessing superclass members.

Signup and view all the flashcards

What is Single Inheritance?

A type of inheritance where one class inherits from a single superclass.

Signup and view all the flashcards

What is Multilevel Inheritance?

A type where a class inherits from a superclass, which in turn inherits from another superclass, forming a chain.

Signup and view all the flashcards

What is Hierarchical Inheritance?

A type where multiple classes inherit from a single superclass.

Signup and view all the flashcards

Study Notes

  • Inheritance in Java allows a class (subclass/child) to inherit properties/behaviors (fields/methods) from another class (superclass/parent).
  • It facilitates code reusability and method overriding.

Basic Syntax

  • extends is the keyword used to implement inheritance.
  • A Parent class contains a display() method.
  • A Child class extends the Parent class and contains a show() method.
  • The Child class inherits the display() method from the Parent class.

Key Terms

  • extends: Keyword to create inheritance.
  • Superclass: The class being inherited (Parent).
  • Subclass: The class that inherits (Child).
  • super: Keyword to refer to the superclass.

Types of Inheritance in Java

  • Single, Multilevel, and Hierarchical inheritance are supported in Java.
  • Multiple inheritance is not supported for classes directly but can be achieved through interfaces.

Example of Multilevel Inheritance

  • Class B inherits from Class A.
  • Class C inherits from Class B.
  • Class A has method methodA().
  • Class B has method methodB().
  • Class C has method methodC().

super Keyword Example

  • The super keyword is used to call the superclass's method.
  • In the Child class, super.display() calls the display() method of the Parent class.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team
Use Quizgecko on...
Browser
Browser