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:
TeachingStaffextendsStaff→ inherits fields (name,staffid) and methods (displayDetails()).- Use of
superto 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:
SavingsAccountextendsBankAccount.- Overrides
displayAccountInfo()to includeinterestRate. super.displayAccountInfo()calls the parent method before adding extra info.- Demonstrates inheritance + polymorphism (method overriding).
Week 9 Concepts Summary
-
Inheritance
-
Child class inherits fields and methods from parent class.
- Use
extendskeyword. -
Method Overriding
-
Child class provides a new implementation of a parent class method.
- Use
@Overrideannotation for clarity. -
superKeyword -
Call parent constructor:
super(args). - Call parent method:
super.methodName(). -
Encapsulation & Reuse
-
Reuse parent class code while extending functionality in child classes.