Fiveable
Fiveable

extends

Definition

In Java, the keyword "extends" is used to create a subclass that inherits properties and methods from a superclass. It establishes an "is-a" relationship between classes.

Analogy

Think of a superhero and their sidekick. The sidekick extends the abilities and powers of the superhero, inheriting their skills and knowledge.

Related terms

Subclass: A class that extends another class to inherit its properties and methods.

Superclass: The class that is being extended or inherited from.

Inheritance: The process by which one class acquires the properties and behaviors of another class.

"extends" appears in:

Practice Questions (20)

  • If class C extends class H, class B extends class H, and class G extends class B, what is the relationship between H and G?
  • Given the following code: ``` class A { public static void show() { System.out.print("A"); } } class B extends A { public static void show() { System.out.print("B"); } } class C extends B { public static void show() { System.out.print("C"); } } ``` What will be the output when the following code is executed? ``` A obj1 = new C(); B obj2 = new C(); C obj3 = new C(); obj1.show(); obj2.show(); obj3.show(); ```
  • Given the following code: ``` class A { public void display() { System.out.print("A"); } } class B extends A { public void display() { System.out.print("B"); } } class C extends B { public void display() { System.out.print("C"); } } ``` What will be the output when the following code is executed? ``` B obj1 = new C(); C obj2 = new C(); A obj3 = new C(); obj1.display(); obj2.display(); obj3.display(); ```
  • Given the following code: ``` class A { public void show() { System.out.println("A"); } } class B extends A { public void show() { System.out.println("B"); } } class C extends B { public void show() { System.out.println("C"); } } ``` What will be the output when the following code is executed? ``` A obj = new C(); obj.show(); ```
  • Consider the following class hierarchy: ``` class Animal { void makeSound() { System.out.println("Some sound"); } } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } } class Cat extends Animal { void makeSound() { System.out.println("Meow"); } } ``` If we create an object like this: `Animal animal = new Dog();`, what will be printed when we call `animal.makeSound()`?
  • Given the following code: ``` class A { public static void printMessage() { System.out.println("Hi from A"); } } class B extends A { public static void printMessage() { System.out.println("Hi from B"); } } ``` What will be the output when the following code is executed? ``` B obj = new B(); obj.printMessage(); ```
  • What will be the output when the following code is executed? ``` public class A { public void print() { System.out.println("A"); } } public class B extends A { public void print() { System.out.println("B"); } } public class C extends B { public void print() { System.out.println("C"); } } public class Main { public static void main(String[] args) { A obj = new C(); obj.print(); } } ```
  • Given the following code: ``` class A { public void show() { System.out.println("A"); } } class B extends A { public void show() { System.out.println("B"); } } ``` What will be the output when the following code is executed? ``` A obj1 = new A(); B obj2 = new B(); A obj3 = new B(); obj1.show(); obj2.show(); obj3.show(); ```
  • Given the block of code below, answer the following question: ```java class Person { // constructor not shown public void printGreeting() { System.out.println("Hello! How are you?"); } } class Student extends Person { // constructor not shown } ``` Question: Consider the code above. If we create an object of the `Student` class and call the `printGreeting()` method on that object, what will be outputted?
  • "Given the block of code below, answer the following question: ```java class Shape { // constructor not shown public void draw() { System.out.println("Drawing a shape"); } } class Circle extends Shape { // constructor not shown public void draw() { System.out.println("Drawing a circle"); super.draw(); } } ``` Question: Consider the code above. If we create an object of the Circle class and call the `draw()` method on that object, what will be the output?
  • Consider the code below. If we create an object of the B class and call the `printMessage()` method on that object, what will be the output? ```java class B { void printMessage() { super.printMessage(); System.out.println("Hello from B"); } } class A extends B { void printMessage() { System.out.println("Hello from A"); } } ```
  • Consider the code below. If we create an object of the Child class and call the `printMessage()` method on that object, what will happen? ```java class Parent { void printMessage() { System.out.println("Hello from Parent"); } } class Child extends Parent { void printMessage() { printMessage(); System.out.println("Hello from Child"); } } ```
  • If Class D extends Class B, and Class C extends Class D, what is the relationship between Class C and Class B?
  • What output do we get if we run the following code? class X { public X() { System.out.println("X's no-argument constructor is invoked"); } } class Y extends X { public Y() { } } public class Z { public static void main(String[] args) { Y y = new Y(); } }
  • What happens when we try to run the following? class D { public D(int x) { } } class E extends D { public E() { } } public class C { public static void main(String[] args) { E e = new E(); } }
  • Given the block of code below, answer the following question: ```java class Shape { // constructor not shown public void draw() { System.out.println("Drawing a shape"); } } class Circle extends Shape { // constructor not shown @Override public void draw() { System.out.println("Drawing a circle"); } } ``` Question: Consider the code above. If we create an object of the Circle class and call the `draw()` method on that object, what will be the output?
  • Given the block of code below, answer the following question: ```java class Vehicle { // constructor not shown public void startEngine() { System.out.println("Vrooom!"); } } class Car extends Vehicle { // constructor not shown public void reverse() { System.out.println("Beep! Beep!"); } } ``` Question: Consider the code above. If we create an object of the `Car` class and call the `startEngine()` method on that object, what will be outputted?
  • Given the block of code below, answer the following question: ```java class Animal { // constructor not shown } class Dog extends Animal { // constructor not shown public void makeSound() { System.out.println("Woof! Woof!"); } } ``` Question: Consider the code above. If we create an object of the `Dog` class and call the `makeSound()` method on that object, what will be outputted?
  • What does the class header “public class B extends C” mean?
  • If classes E and F are created using the headers “public class E extends C” and “public class F extends C” respectively, what is true about them?


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.