Advanced Java Programming · Unit 1
Object-Oriented Programming Fundamentals
Exam-focused notes for Object-Oriented Programming Fundamentals (Advanced Java Programming, BIT401): what the TU syllabus asks and how it has actually been tested, with 7 solved past questions from this unit.
Object-Oriented Programming Fundamentals in short
Object-oriented programming (OOP) builds a program from classes and objects. This chapter covers the Java features that give OOP its power: overloading, overriding, inheritance, interfaces, inner classes, packages and the Object class.
What this unit covers
- Method overloading and overriding
- Inheritance and class hierarchies
- Interface design and implementation
- Abstract classes and polymorphism
- Constructor chaining
- Inner classes and anonymous inner classes
- Package definition and organization
- Final keyword and immutability
- Object class and its methods
Inner classes and anonymous inner classes
2081 · 10 marks
How do you create inner class and anonymous inner class? Create a class EMPLOYEE with data members Emp_ID, Name and Occupation, and write those objects to the file DOC.DAT whose occupations is doctor.[10]
Package definition and organization
2081 · 5 marks
Define package. What is the use of final keyword and finally block? [5]
Interface design and implementation
2080 · 10 marks
Why do we need interface? Define an interface named shape with area ( ) method. Create a class named Rectangle with length and breadth circle with radius that implements shape interface. Now create the object of both classes and display their area.[10]
Constructor chaining
2080 · 5 marks
Describe the chain of constructor with example. [5]
Final keyword and immutability
2080 · 5 marks
Write short notes on a. Final keyword b. Object class [5]
Method overloading and overriding
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(), 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]
Show solved answerHide answer
Method Overriding and Multilevel Inheritance - Model Answer
Concept of Method Overriding
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.
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().
0 · 5 marks
What is method overloading? Explain with example. [5]
Show solved answerHide answer
Method Overloading
Definition
Method overloading is a feature of Object-Oriented Programming that allows a class to have multiple methods with the same name but different parameters. It is a way to implement compile-time (static) polymorphism.
The methods must differ in:
- Number of parameters, OR
- Type of parameters, OR
- Order of parameters
Rules for Method Overloading
- Methods must have the same name
- Methods must have different parameter lists
- Return type alone is NOT sufficient for overloading
- Access modifiers can be different
Example
class Calculator {
// Method 1: Add two integers
public int add(int a, int b) {
return a + b;
}
// Method 2: Add three integers (different number of parameters)
public int add(int a, int b, int c) {
return a + b + c;
}
// Method 3: Add two doubles (different type of parameters)
public double add(double a, double b) {
return a + b;
}
// Method 4: Add int and double (different order/type)
public double add(int a, double b) {
return a + b;
}
}
// Usage
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); // Calls Method 1: 15
System.out.println(calc.add(5, 10, 15)); // Calls Method 2: 30
System.out.println(calc.add(5.5, 10.5)); // Calls Method 3: 16.0
System.out.println(calc.add(5, 10.5)); // Calls Method 4: 15.5
}
}
Advantages
- Improves code readability and usability
- Makes the API intuitive (same operation, same method name)
- Reduces confusion with multiple method names for similar operations
Make Unit 1 stick
Practice BIT401 with flashcards & quizzes