Skip to lesson

Why a class can extend only one class

In Java, a class can extend only one class. Having two or more direct parent classes is called multiple inheritance, and Java does not allow it for classes.

One class, one parent

A smartphone is a phone and also a camera, so you try class SmartPhone extends Phone, Camera. Java refuses: after extends comes exactly one class name, the class's only direct parent.

Example 1 · Two parents: a compile error

TwoParentsDemo.javaJava
class Phone {    String brand;}
class Camera {    int megapixels;}
class SmartPhone extends Phone, Camera {}
public class TwoParentsDemo {    public static void main(String[] args) {        SmartPhone sp = new SmartPhone();    }}
Sample compiler output
TwoParentsDemo.java:9: error: '{' expected
class SmartPhone extends Phone, Camera {
                              ^
1 error

What this code does

  • After extends Phone, javac expects the class body {, so the comma is an error.
  • The message does not say "multiple inheritance". Learn to read it: one name after extends, then {.

Example 2 · A class cannot be its own ancestor

CycleDemo.javaJava
class Teacher extends Staff {}
class Staff extends Teacher {}
public class CycleDemo {    public static void main(String[] args) {        System.out.println("Hi");    }}
Sample compiler output
CycleDemo.java:1: error: cyclic inheritance involving Teacher
class Teacher extends Staff {
^
1 error

What this code does

  • Here Teacher would be above Staff and below it at the same time. Javac calls this cyclic inheritance.
  • A family must start at one top class and go only downwards.

The limit is only about direct parents. A class may still have many ancestors through a chain, as in multilevel inheritance (opens in a new tab), and a parent may have many children. Each class just has one parent right above it.

The clash problem

Phone has a start() that opens the dialer, and Camera has a start() that opens the lens. If SmartPhone had both parents, sp.start() would have two possible meanings.

Example 3 · One parent: no doubt which start() runs

OneParentDemo.javaJava
class Phone {    void start() {        System.out.println("Phone: opening dialer");    }}
class Camera {    void start() {        System.out.println("Camera: opening lens");    }}
class SmartPhone extends Phone {}
public class OneParentDemo {    public static void main(String[] args) {        SmartPhone sp = new SmartPhone();        sp.start();        Camera c = new Camera();        c.start();    }}
Output
Phone: opening dialer
Camera: opening lens

What this code does

  • Phone and Camera both have start(). SmartPhone has one parent, so sp.start() can only mean Phone's method.
  • With two parents, sp.start() would have two possible answers. That is the clash Java avoids.

Fields clash the same way: if both parents had a brand, which one would the child see? Java would have to guess, or make you write extra rules for every clash. Its designers chose a simpler rule: one parent, so there is never a doubt.

The diamond problem

Now give both parents a common parent: Phone and Camera both extend Device, which has a battery field. A SmartPhone with both parents would reach Device by two paths. Does it hold one battery or two, and whose changes win?

The diamond problem. Phone and Camera both extend Device, which has int battery = 100. If SmartPhone could extend both Phone and Camera, it would reach Device by two paths, and nobody could say whether it holds one battery or two. Java does not allow it for classes.

Drawn on paper, the four classes make a diamond, so this is called the diamond problem. Some languages, like C++, allow it and add special rules to settle it. Java avoids the whole problem for classes.

What to do instead

First, check the IS-A test. If the classes really form a chain, use multilevel inheritance: SmartPhone extends Phone, and Phone extends Device. SmartPhone gets both levels, and each class still has one parent.

Example 4 · A chain gives members of two classes

ChainFixDemo.javaJava
class Device {    int battery = 100;
    void use(int percent) {        battery = battery - percent;    }}
class Phone extends Device {    void call(String name) {        use(5);        System.out.println("Called " + name + ", battery " + battery);    }}
class SmartPhone extends Phone {    void watchVideo() {        use(20);        System.out.println("Watched video, battery " + battery);    }}
public class ChainFixDemo {    public static void main(String[] args) {        SmartPhone sp = new SmartPhone();        sp.call("Rohan");        sp.watchVideo();    }}
Output
Called Rohan, battery 95
Watched video, battery 75

What this code does

  • SmartPhone gets call() from Phone and battery and use() from Device.
  • There is only one path to Device, so there is exactly one battery field. No diamond.

Example 5 · Extend the main parent, add the rest

AthleteDemo.javaJava
class Student {    String name;    int marks;}
class StudentAthlete extends Student {    String sport;    int medals;
    void showProfile() {        System.out.println(name + ": " + marks + " marks");        System.out.println(sport + ", " + medals + " medals");    }}
public class AthleteDemo {    public static void main(String[] args) {        StudentAthlete a = new StudentAthlete();        a.name = "Diya";        a.marks = 84;        a.sport = "Badminton";        a.medals = 3;        a.showProfile();    }}
Output
Diya: 84 marks
Badminton, 3 medals

What this code does

  • A student athlete is a student, so Student is the parent.
  • The athlete parts (sport, medals) are its own fields, so it never needs a second parent class.

If a class truly is two kinds of thing, extend the main one and keep the rest as its own fields and methods: a student athlete is a Student that also has a sport and medals. Java's full answer to "two kinds of thing" is interfaces: a class may use many of them, as Implement several interfaces (opens in a new tab) shows.

Fix the error

Fix the one-parent error

Read the error, then pick the line that fixes it.

Round 1 of 3

A laptop is a computer that also has a touch screen.

class Computer {
void boot() { System.out.println("Booting"); }
}
class Tablet {
void touch() { System.out.println("Touch on"); }
}
class Laptop extends Computer, Tablet {
}

Error

Laptop.java:7: error: '{' expected

Next step

Check your understanding

11 quick questions