Skip to content

Week 9: Java – Inheritance & Method Overriding


Q1. TeachingStaff – Inheritance Example

Objective: Create a TeachingStaff class that inherits from Staff and adds extra attributes.

import java.util.*;

class Staff {
    String name;
    String staffid;

    public Staff(String name, String staffid) {
        this.name = name;
        this.staffid = staffid;
    }

    public void displayDetails() {
        System.out.println("Name: " + name + "\nStaff ID: " + staffid);
    }
}

class TeachingStaff extends Staff {
    String subject;
    int YOE; // Years of Experience

    public TeachingStaff(String name, String staffid, String subject, int YOE) {
        super(name, staffid);
        this.subject = subject;
        this.YOE = YOE;
    }

    public void showTeachingInfo() {
        super.displayDetails();
        System.out.println("Subject: " + subject + "\nYears of Experience: " + YOE + " years");
    }
}

public class q1 {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter name: ");
        String name = sc.nextLine();
        System.out.print("Enter staff id: ");
        String staffid = sc.nextLine();
        System.out.print("Enter subject: ");
        String subject = sc.nextLine();
        System.out.print("Enter years of experience: ");
        int yoe = sc.nextInt();

        TeachingStaff staff = new TeachingStaff(name, staffid, subject, yoe);
        staff.showTeachingInfo();
    }
}

Key Points:

  • TeachingStaff extends Staff → inherits fields (name, staffid) and methods (displayDetails()).
  • Use of super to call the parent class constructor and methods.
  • Adds new fields (subject, YOE) and a method (showTeachingInfo) for extended functionality.

Q2. SavingsAccount – Method Overriding

Objective: Demonstrate method overriding by extending BankAccount.

import java.util.Scanner;

class BankAccount {
    String accountHolder;
    double balance;

    BankAccount(String accountHolder, double balance) {
        this.accountHolder = accountHolder;
        this.balance = balance;
    }

    void displayAccountInfo() {
        System.out.println("Account Holder: " + accountHolder);
        System.out.println("Balance: " + balance);
    }
}

class SavingsAccount extends BankAccount {
    double interestRate;

    SavingsAccount(String accountHolder, double balance, double interestRate) {
        super(accountHolder, balance);
        this.interestRate = interestRate;
    }

    @Override
    void displayAccountInfo() {
        super.displayAccountInfo(); // Call parent method
        System.out.println("Interest Rate: " + interestRate + "%");
    }
}

public class q2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter account holder name: ");
        String name = sc.nextLine();
        System.out.print("Enter balance: ");
        double balance = sc.nextDouble();
        System.out.print("Enter interest rate: ");
        double rate = sc.nextDouble();

        SavingsAccount account = new SavingsAccount(name, balance, rate);
        account.displayAccountInfo();
    }
}

Key Points:

  • SavingsAccount extends BankAccount.
  • Overrides displayAccountInfo() to include interestRate.
  • super.displayAccountInfo() calls the parent method before adding extra info.
  • Demonstrates inheritance + polymorphism (method overriding).

Week 9 Concepts Summary

  1. Inheritance

  2. Child class inherits fields and methods from parent class.

  3. Use extends keyword.
  4. Method Overriding

  5. Child class provides a new implementation of a parent class method.

  6. Use @Override annotation for clarity.
  7. super Keyword

  8. Call parent constructor: super(args).

  9. Call parent method: super.methodName().
  10. Encapsulation & Reuse

  11. Reuse parent class code while extending functionality in child classes.


Comments