Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

The extends and implements keywords

Reference for Java extends and implements: subclass a class, implement interfaces, extend interfaces, bound generics, and understand overriding and the Liskov rules.

extends creates an inheritance relationship. A class can extend exactly one class, an interface can extend any number of interfaces, and a generic type parameter can be bounded with extends. implements is used only by classes (and enums and records) to declare the interfaces they fulfil.

A subclass inherits the accessible fields and methods of its parent and can override methods to change behavior. A class that implements an interface must provide all of its abstract methods, unless the class is abstract.

Java
class Dog extends Animal implements Comparable<Dog> { }
interface Pet extends Named, Owned { }
<T extends Number> double sum(List<T> list) { }

Try it

Java Extending a class and implementing an interface

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Interface extends, bounds and static types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
class Sub extends Super implements I1, I2 { }
interface Sub extends I1, I2 { }
class Box<T extends Number & Comparable<T>> { }
Form Rule
class A extends B B must be a non-final class. Single inheritance only. Default parent is Object.
class A implements I, J Implement every abstract method, with public access.
interface I extends J, K Multiple interface inheritance is allowed.
<T extends B & I> Bounds: at most one class, listed first, then interfaces.
Enums, records Implicitly extend Enum / Record, so they only use implements.

Usage notes

  • Constructors are not inherited. Each subclass constructor calls a parent constructor with super(...), explicitly or implicitly (the no-argument one).
  • Overriding rules: same signature, equal or wider access, compatible (covariant) return type, no broader checked exceptions. Use @Override so mistakes become compile errors.
  • Fields are hidden, not overridden; which field you see depends on the reference type. Avoid reusing field names.
  • Static methods are hidden, not overridden; see the second example.
  • Prefer composition when the relationship is "has a", not "is a". Inheritance couples subclass to the parent's implementation.
  • Liskov substitution: a subclass must be usable wherever the parent is expected without surprising the caller.
  • sealed and final control who may extend a type; see sealed and final.
  • Wildcards use extends too: List<? extends Number> is read-only for adding elements.
  • Same default method from two interfaces must be resolved by overriding it in the class.