Why a class can extend only one class
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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
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(); }}TwoParentsDemo.java:9: error: '{' expected
class SmartPhone extends Phone, Camera {
^
1 errorWhat 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
class Teacher extends Staff {}
class Staff extends Teacher {}
public class CycleDemo { public static void main(String[] args) { System.out.println("Hi"); }}CycleDemo.java:1: error: cyclic inheritance involving Teacher
class Teacher extends Staff {
^
1 errorWhat this code does
- Here
Teacherwould be aboveStaffand 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
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(); }}Phone: opening dialer Camera: opening lens
What this code does
PhoneandCameraboth havestart().SmartPhonehas one parent, sosp.start()can only meanPhone'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?
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
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(); }}Called Rohan, battery 95 Watched video, battery 75
What this code does
SmartPhonegetscall()fromPhoneandbatteryanduse()fromDevice.- There is only one path to
Device, so there is exactly onebatteryfield. No diamond.
Example 5 · Extend the main parent, add the rest
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(); }}Diya: 84 marks Badminton, 3 medals
What this code does
- A student athlete is a student, so
Studentis 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.
Error
Laptop.java:7: error: '{' expectedNext step
Check your understanding
11 quick questions