Search⌘ K
AI Features

Inheritance Gotchas

Explore common pitfalls in Java inheritance, including how instance methods are overridden, the behavior of static methods, and conflicts with interface default methods. Understand access specifier rules and compiler errors related to overriding and hiding methods. This lesson helps clarify nuanced behaviors to improve your Java coding and debugging skills in object-oriented design.

We'll cover the following...
Technical Quiz
1.

Consider the setup below:

public class Language {

    static String lang = "base language";

    static protected void printLanguage() {
        System.out.println(lang);
    }

    protected Language sayHello() {
        System.out.println("----");
        return this;
    }
}

public class Spanish extends Language {

    static String lang = "Spanish";

    static protected void printLanguage() {
        System.out.println(lang);
    }

    protected Language sayHello() {
        System.out.println("Ola!");
        return this;
    }
}

What would be the outcome of the below code snippet?

(new Spanish()).sayHello()
A.

“Ola”

B.

“----”


1 / 3
Java
class Demonstration {
public static void main( String args[] ) {
(new Spanish()).sayHello();
Language lg = new Spanish();
lg.sayHello();
}
}
class Language {
static String lang = "base language";
static protected void printLanguage() {
System.out.println(lang);
}
protected Language sayHello() {
System.out.println("----");
return this;
}
}
class Spanish extends Language {
static String lang = "Spanish";
static protected void printLanguage() {
System.out.println(lang);
}
protected Language sayHello() {
System.out.println("Ola!");
return this;
}
}
Technical Quiz
1.

Consider the class setup below:

public interface Vehicle {

    default void whatAmI() {
        System.out.println("I am a vehicle");
    }
}
public interface SevenSeater extends Vehicle {}

public interface SUV extends Vehicle {

    default void whatAmI() {
        System.out.println("I am a SUV");
    }
}

public class TeslaModelX implements SUV, SevenSeater {

    public void identifyMyself() {
        whatAmI();
    }
}

What will be the output of (new TeslaModelX()).identifyMyself()

A.

will not compile

B.

I am a SUV

C.

I am a vehicle


1 / 1
Java
class Demonstration {
public static void main( String args[] ) {
(new TeslaModelX()).identifyMyself();
}
}
interface Vehicle {
default void whatAmI() {
System.out.println("I am a vehicle");
}
}
interface SevenSeater extends Vehicle {}
interface SUV extends Vehicle {
default void whatAmI() {
System.out.println("I am a SUV");
}
}
class TeslaModelX implements SUV, SevenSeater {
public void identifyMyself() {
whatAmI();
}
}
Technical Quiz
1.

Consider the class setup below:

public interface SuperPower {

    void fly();
}

public class JetPack {

    public void fly() {
        System.out.println("fly away");
    }
}

public class FlyingMan extends JetPack implements SuperPower {

    void identify() {
        System.out.println("I am a flying man.");
    }
}

The class FlyingMan implements the interface SuperPower but doesn’t provide an implementation for it directly. Will this code snippet compile since the class JetPack has a fly method?

A.

No

B.

Yes


1 / 1
Java
class Demonstration {
public static void main( String args[] ) {
(new FlyingMan()).fly();
}
}
interface SuperPower {
void fly();
}
class JetPack {
public void fly() {
System.out.println("fly away");
}
}
class FlyingMan extends JetPack implements SuperPower {
void identify() {
System.out.println("I am a flying man.");
}
}
Technical Quiz
1.

Consider the two classes below:

class Parent {
    public void dummyMethod(){

    }
}

class Child extends Parent {
    protected void dummyMethod(){

    }
}

What is wrong with the above code?

A.

Both the class declarations are missing the access modifiers

B.

The overridden method dummyMethod in the Child class is specified a more restrictive access modifier than in the Parent class.

C.

dummyMethod has no method body


1 / 1

Takeaways

  • Instance methods in base classes can be overridden by derived classes.
  • If a subclass defines a static method
...