Difficulty: Intermediate | Prerequisites: Basic Java syntax, classes, methods, constructors
Object-oriented programming in Java is built on the idea that you can define general categories of things (classes) and then create more specialised versions of them. Inheritance and interfaces are the two mechanisms Java gives you for doing this. Inheritance lets a subclass borrow behaviour from a parent class, while interfaces let unrelated classes promise they can all do the same thing. Together, they make polymorphism possible, which is the reason large Java programmes can stay maintainable as they grow. You should already be comfortable writing basic classes, constructors, and instance methods before working through this material.
Inheritance lets one class extend another to reuse code and add specialisation. Interfaces define a contract of methods that any implementing class must provide. Polymorphism means a single variable type can refer to objects of different classes, and the correct method runs based on the actual object at runtime.
Object
A specific instance of a class, carrying its own set of attribute values (state) and access to the class's methods (behaviour). Think of it as one particular thing built from a class blueprint.
Class
A template that defines the attributes and behaviours shared by all objects of that type. In simple terms, it is the blueprint; the object is the house.
Instantiation
The act of creating an object from a class by calling a constructor with the new keyword. Think of it as "building the house from the blueprint."
Constructor
A special method that initialises a new object. It shares the class's name and has no return type.
Overloading
Defining multiple methods (or constructors) with the same name but different parameter lists. For example, Student(String name) and Student(String name, int age) are overloaded constructors. In simple terms, same name, different inputs.
Instance variable
A variable that belongs to a specific object, not to a method or the class as a whole. It persists for the lifetime of the object.
Dot operator (dereferencing)
The . used to call non-static methods or access fields on an object, e.g. movie.title.
Inheritance
A mechanism where a subclass automatically receives the fields and methods of a superclass. Think of it as a child class getting features from its parent for free.
Superclass (parent class)
The class being extended. It sits higher in the hierarchy.
Subclass (child class)
The class that extends a superclass and inherits its behaviour. It can add new methods or override existing ones.
extends keyword
The Java keyword a subclass uses to declare it inherits from a superclass: class Lawyer extends Employee.
super keyword
Used inside a subclass to call the parent class's constructor (super()) or an overridden method (super.method()).
Is-a relationship
A hierarchical connection meaning one type can be treated as a specialised version of another. A Lawyer "is-a" Employee.
Interface
A contract that lists method signatures a class must implement. It cannot be instantiated on its own and contains no instance variables or constructors.
implements keyword
Declares that a class will provide concrete bodies for every method in a given interface.
instanceof operator
A Java keyword that evaluates to a boolean, checking whether an object is of a particular type at runtime. Example: if (o instanceof Point).
Polymorphism
The ability of a single variable type to refer to objects of multiple classes, with the correct overridden method being called at runtime. In simple terms, one reference, many possible forms.
@Override annotation
Placed above a method to signal that it intentionally replaces a superclass method. The compiler will flag an error if the method does not actually override anything.
Abstract class
A class that cannot be instantiated directly. It may contain both concrete methods and abstract (unimplemented) method signatures. Subclasses must provide implementations for the abstract methods.
An object is a specific instance of a class, bundling state (attributes) and behaviour (methods).
A class defines what attributes and behaviours its objects share.
You create an object by calling a constructor with new, passing appropriate arguments.
Constructors can be overloaded: same name, different parameter lists.
Student(String name)
Student(String name, int age)
The dot operator calls non-static methods on an object (dereferencing).
Instance variables belong to the object, not to any single method. They are not local variables.
Instance variables cannot be accessed inside a static method directly. To reach them from a static context, you need an object reference: movie.title.
When you create an object with new, the object itself is the implicit parameter available inside its own methods.
Use the extends keyword to create a subclass: class Lawyer extends Employee.
The subclass inherits all non-private fields and methods from its superclass.
Constructors are not inherited. A subclass receives a default constructor unless you explicitly call super() (with or without arguments) to invoke the parent's constructor.
Private instance variables are not directly accessible in the subclass. Use getter methods to reach them.
The super keyword lets a subclass call overridden superclass methods:
public double getSalary() { double sal = super.getSalary(); // add bonus, etc. return sal;}
Inheritance serves two purposes: code reuse (avoid duplicating logic) and polymorphism (treat different subclasses through a common type).
An interface is a list of method signatures that a class promises to implement.
A class declares this with the implements keyword.
The implementing class must provide concrete bodies for every method declared in the interface.
Interfaces cannot be instantiated. You cannot write Controller myControl = new Controller() if Controller is an interface.
Interfaces contain no instance variables and no constructors.
What interfaces can contain:
Abstract methods (implicitly public abstract; writing abstract explicitly is optional)
public static final fields (constants)
Default methods (have a body, introduced in Java 8)
Static methods (have a body)
Interfaces enable polymorphism: unrelated classes implementing the same interface can be handled through a single reference type.
A variable declared as a superclass or interface type can hold a reference to any subclass or implementing object.
Employee ed = new Lawyer();
When you call a method on ed, the version that runs is determined by the actual object (Lawyer), not the declared type (Employee). This is runtime dispatch.
The compiler checks whether the declared type has the method. It does not know the actual type at compile time, which is why you sometimes need casting.
Use instanceof to check the actual type before casting:
if (o instanceof Point) { Point other = (Point) o;}
When overriding a method, use the @Override annotation so the compiler can verify correctness.
An abstract class sits between a regular class and an interface. It cannot be instantiated directly, but it can hold concrete method implementations, instance variables, and constructors.
Abstract classes can use declared (static) types for polymorphism, just as interfaces can.
If a class is not an interface, you must explicitly write the abstract keyword on any methods without a body.
The inheritance and interface model maps directly to how large software systems are structured. A payment processing system, for example, might define a PaymentMethod interface and have CreditCard, BankTransfer, and DigitalWallet classes each implement it. The rest of the codebase only needs to know about PaymentMethod, which keeps things flexible when a new payment type is added.
Students often think a subclass inherits the parent's constructor. It does not. You must call super() explicitly, or the subclass gets only a default no-argument constructor.
Students sometimes assume that private instance variables in the parent are directly available in the subclass. They are not. You need getter methods.
Declaring Employee ed = new Lawyer() does not mean ed behaves as an Employee. The methods that run are Lawyer's overridden versions. The declared type only controls what the compiler allows you to call.
Writing abstract in front of methods inside an interface is allowed but unnecessary. In a regular (non-interface) class, you do need the keyword.
⚠️ Know the difference between what the compiler checks (declared type) and what runs at runtime (actual object type). This is the core of polymorphism questions.
⚠️ Be ready to trace method calls through an inheritance hierarchy: which version of an overridden method executes?
⚠️ Understand exactly what an interface can and cannot contain (no instance variables, no constructors, but can have default and static methods).
⚠️ Constructors are not inherited. Expect a question on when super() must be called.
True or False: A subclass automatically inherits the constructor of its superclass. False. Constructors are never inherited. Use super() to call the parent's constructor.
True or False: An interface can contain instance variables. False. Interfaces can only contain public static final fields (constants).
Fill in the blank: The ________ operator checks whether an object is of a given type and evaluates to a boolean. instanceof
True or False: If Employee ed = new Lawyer(), calling ed.getSalary() runs the Employee version of getSalary(). False. The Lawyer version runs, because the actual object is a Lawyer.
Fill in the blank: To call a superclass's overridden method from within the subclass, use ________.methodName(). super
Q: What is the difference between a superclass and a subclass?
A: A superclass is the parent class being extended. A subclass is the child class that extends the superclass and inherits its non-private fields and methods.
Q: Why can you not instantiate an interface?
A: An interface only defines method signatures (a contract). It has no constructor and no instance state, so there is nothing concrete to create an object from.
Q: Given Employee ed = new Lawyer();, explain what happens when ed.getPinkSlip() is called, assuming both Employee and Lawyer define that method.
A: The Lawyer version of getPinkSlip() executes. At runtime, Java uses the actual object type (Lawyer), not the declared type (Employee), to resolve which overridden method to call.
Q: What is the purpose of the @Override annotation?
A: It tells the compiler that the method is intended to override a superclass method. If no matching method exists in the parent, the compiler raises an error, catching typos and signature mismatches early.
Q: Can an abstract class be instantiated? Can it have concrete methods?
A: No, it cannot be instantiated directly. Yes, it can have both abstract methods (no body) and concrete methods (with a body), plus instance variables and constructors.
This material connects directly to the Comparable interface and sorting algorithms, which rely on polymorphism to sort objects of any type that implements compareTo. It also sets the foundation for data structures like linked lists and trees, where nodes often use inheritance or interfaces to handle different element types through a common reference.
OOP, object-oriented programming, Java inheritance, extends keyword, superclass, subclass, parent class, child class, interface, implements, polymorphism, method overriding, @Override, instanceof, abstract class, is-a relationship, super keyword, constructor chaining, runtime dispatch, dynamic binding, compile-time type, runtime type, dereferencing, dot operator, overloading, CSCE, Texas A&M, intro to CS