CENTRE FOR DISTANCE AND ONLINE EDUCATION
CENTRE FOR DISTANCE AND ONLINE EDUCATION
LIVE SESSION – SESSION NO. 5
O02LA502– JAVA PROGRAMMING LAB.
MASTER OF COMPUTER APPLICATION
JOYASHRI BASAK
ASSISTANT PROFESSOR
At the end of this session, you should be able to:
Learning
• Demonstrate use of method overriding
Objectives • Implementation code of it.
Exercises in Java
1 Write a Java program to display employee details (employee name, salary) using class and method invocation
2 Write a java program to demonstrate the use of (i) increment (++) and decrement operator (--) (ii) IF-ELSE
Statement (iii) While loop.
3 Write a java program to perform matrix multiplication using 2-dimenstional array
4 Write a Java Program to check whether given String is Palindrome or not
5 Write a java program to demonstrate the use of super class and sub class concept.
6 Write a Java program using exception handling methods
7 Write a Java program to create a class and a subclass and demonstrate that a method can override.
8 Write a Java program to demonstrate the use of byte stream.
9. Write a Java program to create and start multiple threads that increment a shared counter variable
concurrently.
Exercise 7
Write a Java program to create a class and a subclass and demonstrate that
a method can override.
Exercise 7
Write a Java program to create a class and a subclass and demonstrate that
a method can override.
What is Method Overriding?
Method overriding in Java occurs when a subclass (child class) provides a specific
implementation of a method that is already defined in its superclass (parent class).
•It allows a subclass to modify the behavior of an inherited method.
•It is used to achieve runtime polymorphism.
Exercise 7
Write a Java program to create a class and a subclass and demonstrate that
a method can override.
• Rules for Method Overriding:
• The method in the subclass must have the same name, return type, and parameters
as in the [Link] method must be inherited from the [Link] access
modifier of the overriding method cannot be more restrictive than the overridden
[Link] overriding method can have a wider or same access [Link] the
@Override annotation to indicate that you are overriding a method (optional but
recommended).
Exercise 7
Write a Java program to create a class and a subclass and demonstrate that
a method can override.
Class and Subclass:
• A class in Java is a blueprint that defines the properties and behavior of an object.
• A subclass is a class that inherits from another class using the extends keyword.
• The subclass inherits all non-private members (fields and methods) from the
superclass.
• Subclasses can also define new members and override methods from the
superclass.
Exercise 7
Write a Java program to create a class and a subclass and demonstrate that
a method can override.
Why Use Method Overriding?
• To customize behavior for specific types while reusing the general [Link]
implement dynamic method dispatch (deciding at runtime which version of the
method to execute).To provide specific implementations in subclasses that differ
from the superclass.
Exercise 7
Write a Java program to create a class and a subclass and demonstrate that
a method can override.
Algorithm
Step 1: Start
Step 2: Define the superclass EmployeeDeclare instance variables name and salaryCreate a constructor to initialize the
variablesDefine a method displayDetails() to print name and salary
Step 3: Define the subclass Manager that extends EmployeeDeclare an additional instance variable bonusCreate a constructor
to initialize name, salary, and bonus using super for inherited fieldsOverride the displayDetails() method to include bonus
information
Step 4: In the main() methodCreate an object of Employee and call displayDetails()Create an object of Manager and call
displayDetails()Create a reference of type Employee that points to a Manager objectCall displayDetails() using the superclass
reference to demonstrate method overriding
Step 5: End
Exercise 7
// Superclass // Overriding the displayDetails() method
class Employee {
@Override
String name;
void displayDetails() {
double salary;
[Link]("Manager Name: " + name); Employee Name: Alice
[Link]("Salary: " + salary);
// Constructor
[Link]("Bonus: " + bonus); Salary: 50000.0
Employee(String name, double salary) {
}
[Link] = name;
}
[Link] = salary; Manager Name: Bob
}
// Main class Salary: 80000.0
public class EmployeeOverrideDemo {
// Method to display employee details
public static void main(String[] args) { Bonus: 15000.0
void displayDetails() {
// Create Employee object
[Link]("Employee Name: " + name);
Employee emp = new Employee("Alice", 50000);
[Link]("Salary: " + salary);
[Link](); Manager Name: Charlie
}
} Salary: 90000.0
[Link]();
Bonus: 20000.0
// Subclass
// Create Manager object
class Manager extends Employee {
Manager mgr = new Manager("Bob", 80000, 15000);
double bonus;
[Link]();
// Constructor
[Link]();
Manager(String name, double salary, double bonus) {
super(name, salary);
// Superclass reference to subclass object
[Link] = bonus;
Employee empRef = new Manager("Charlie", 90000, 20000);
}
[Link](); // Manager's overridden method is called
}
}
Key Learnings
▪ Method Overriding
STUDENT SUPPORT
Thank You !!