Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Lesson 6: Inheritance, super, Overriding & the Object Class

Lesson 7 of 18 in the free Programming in Java notes on Siksha Sarovar, written by Rohit Jangra.

6.1 extends — IS-A Relationships

Inheritance lets a subclass acquire the fields and methods of a superclass, modelling an IS-A relationship (Dog extends Animal). Java supports single class inheritance only (one direct superclass) — the C++ diamond problem for state is designed out; multiple inheritance of type comes via interfaces (Lesson 8).

Types examiners list: single, multilevel (A→B→C), hierarchical (one parent, many children). Multiple and hybrid are supported only through interfaces. Private members are inherited in memory but not accessible; constructors are never inherited.

6.2 super — Three Uses

  1. super.field — access a shadowed superclass field.
  2. super.method() — call the overridden superclass version.
  3. super(args) — invoke a superclass constructor; must be the first statement.

Constructor chaining: every constructor implicitly begins with super() unless you write super(...)/this(...) yourself. If the parent has no no-arg constructor, the child must call super(args) explicitly or compilation fails — a classic trick question.

class A { A() { System.out.println("A ctor"); } }
class B extends A { B() { System.out.println("B ctor"); } }
new B();
// Output:  A ctor  then  B ctor   (parent constructs first, always)

6.3 Overriding vs Overloading — The Definitive Table

AspectOverloadingOverriding
WhereSame class (or inherited set)Subclass redefines parent method
SignatureSame name, different parametersIdentical name + parameters
Return typeCan differ freelySame or covariant (subtype)
BindingCompile time (static)Runtime (dynamic dispatch)
Polymorphism kindCompile-timeRuntime
static methodsCan be overloadedHidden, not overridden
private/finalCan be overloadedCannot be overridden

Overriding rules: access modifier may not be more restrictive (protected → public is fine; public → protected is an error); cannot throw broader checked exceptions; use @Override so the compiler catches typos (e.g., equals(Object) vs a mistaken equals(MyClass) overload).

Fields are never overridden — field access is resolved by the reference type at compile time (field hiding), while instance-method calls are resolved by the object type at runtime. Predict-the-output papers exploit this relentlessly.

6.4 The Object Class — Root of Everything

Every class implicitly extends java.lang.Object. Methods you must know:

MethodPurposeTypical action
toString()String form; default is ClassName@hexHashOverride for readable printing
equals(Object)Logical equality; default is ==Override for value comparison
hashCode()Bucket index for hash collectionsMust override with equals
getClass()Runtime class objectReflection
clone()Field-by-field copy (needs Cloneable)Shallow by default
wait()/notify()/notifyAll()Thread coordination (Lesson 11)Call only inside synchronized
finalize()Pre-GC hookDeprecated

The equals/hashCode contract: if a.equals(b) then a.hashCode() == b.hashCode() must hold. Violate it and your objects vanish inside HashMap/HashSet — an advanced-paper favourite.

6.5 final — Sealing Things

  • final variable: constant after assignment (blank finals may be set once in the constructor). For references, the reference is fixed — the object's contents can still mutate.
  • final method: cannot be overridden (also enables inlining).
  • final class: cannot be extended — e.g., String, Integer, all wrapper classes.

Why is String final? To protect the string pool, its cached hash code, and security-sensitive uses (class loading, file paths) from malicious subclassing.

🎯 Exam Focus

  1. Explain types of inheritance in Java. Why does Java not support multiple inheritance through classes? How do interfaces solve it?
  2. List the three uses of super with examples. Trace constructor-chaining output for a three-level hierarchy.
  3. Differentiate method overloading and method overriding (any six points) with programs.
  4. State the rules of overriding regarding access modifiers, covariant return types and checked exceptions.
  5. Explain the equals()/hashCode() contract. What goes wrong in a HashSet if only equals() is overridden?
  6. Write a program with class Employee (name, salary) and subclass Manager (bonus) that overrides toString() and equals() correctly.