0 2

BIT401 ยท 0 ยท 10 marks

Discuss the concept of method overriding. Write a program to create Person class with variables id, and name. Include methods getPerson() and showPerson() in the class. Again, create another class named Employee, which is child of person class. Include member variable salary, and methods getEmployee

Discuss the concept of method overriding. Write a program to create Person class with variables id, and name. Include methods getPerson() and showPerson() in the class. Again, create another class named Employee, which is child of person class. Include member variable salary, and methods getEmployee(), and showEmployee() in the class. Again, create a third class named Typist, which is child of Employee. Include variable typespeed, and methods getTypist(), and showTypist() in the class. Finally, create two objects of Typist class and read and display their details.[10]

Advanced Java ProgrammingChapter 1: Object-Oriented Programming FundamentalsMethod overloading and overriding

Quick answer

Method Overriding is a feature of Object-Oriented Programming that allows a child class to provide a specific implementation of a method that is already defined in its parent class. When a method in a child class has the same name, return type, and parameters as a method in the parent class, the child class method overrides (replaces) the parent class method.

Method Overriding and Multilevel Inheritance - Model Answer

Concept of Method Overriding

Key Points:

  • Enables runtime polymorphism
  • Child class method must have the same signature as parent class method
  • Allows customized behavior in child classes while maintaining the parent interface
  • Improves code reusability and flexibility

Program Implementation

import java.util.Scanner;

// Parent Class: Person
class Person {
    protected int id;
    protected String name;
    
    public void getPerson() {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter ID: ");
        id = sc.nextInt();
        sc.nextLine();  // consume newline
        System.out.print("Enter Name: ");
        name = sc.nextLine();
    }
    
    public void showPerson() {
        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
    }
}

// Child Class: Employee (inherits from Person)
class Employee extends Person {
    protected double salary;
    
    public void getEmployee() {
        getPerson();  // call parent method
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Salary: ");
        salary = sc.nextDouble();
    }
    
    public void showEmployee() {
        showPerson();  // call parent method
        System.out.println("Salary: " + salary);
    }
}

// Child Class: Typist (inherits from Employee)
class Typist extends Employee {
    private int typespeed;
    
    public void getTypist() {
        getEmployee();  // call parent method
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Typing Speed (words/min): ");
        typespeed = sc.nextInt();
    }
    
    public void showTypist() {
        showEmployee();  // call parent method
        System.out.println("Typing Speed: " + typespeed + " words/min");
    }
}

// Main Class
public class TypistDemo {
    public static void main(String[] args) {
        System.out.println("=== Typist Information System ===\n");
        
        // Create array of Typist objects
        Typist[] typists = new Typist[2];
        
        // Read details for two Typist objects
        for (int i = 0; i < 2; i++) {
            typists[i] = new Typist();
            System.out.println("\n--- Enter Details for Typist " + (i + 1) + " ---");
            typists[i].getTypist();
        }
        
        // Display details for two Typist objects
        System.out.println("\n\n=== Typist Details ===");
        for (int i = 0; i < 2; i++) {
            System.out.println("\n--- Typist " + (i + 1) + " ---");
            typists[i].showTypist();
        }
    }
}

Sample Output

=== Typist Information System ===

--- Enter Details for Typist 1 ---
Enter ID: 101
Enter Name: Ram Kumar
Enter Salary: 25000
Enter Typing Speed (words/min): 60

--- Enter Details for Typist 2 ---
Enter ID: 102
Enter Name: Sita Sharma
Enter Salary: 28000
Enter Typing Speed (words/min): 75

=== Typist Details ===

--- Typist 1 ---
ID: 101
Name: Ram Kumar
Salary: 25000.0
Typing Speed: 60 words/min

--- Typist 2 ---
ID: 102
Name: Sita Sharma
Salary: 28000.0
Typing Speed: 75 words/min

Explanation of Inheritance Hierarchy

Person (Base Class)
  โ†“
Employee (Child of Person)
  โ†“
Typist (Child of Employee - Multilevel Inheritance)
  • Person: Contains basic attributes (id, name) and methods
  • Employee: Extends Person, adds salary attribute and overrides display methods
  • Typist: Extends Employee, adds typespeed attribute and overrides display methods

Each child class calls parent methods using method names, demonstrating method overriding where showEmployee() overrides showPerson(), and showTypist() overrides showEmployee().

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.