SVKM’s NMIMS University
School of Technology Management & Engineering
COURSE: Object Oriented Programming Through JAVA
Experiment: 5
PART A
(PART A: TO BE REFERRED BY STUDENTS)
Aim: To study types of inheritance, method overriding, abstraction-abstract class, abstract
method.
Learning Outcomes: Learner would be able to
1. Understand the syntax of Inheritance in JAVA
2. Interpret the types of inheritance concepts
3. Applying the method overloading and overriding(Dynamic Polymorphism) concepts in
inheritance
Theory:
Inheritance can be defined as the process where one class acquires the properties (methods and
fields) of another. With the use of inheritance, the information is made manageable in a
hierarchical order. The class which inherits the properties of other is known as subclass (derived
class, child class) and the class whose properties are inherited is known as superclass (base class,
parent class).
extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of extends
keyword.
class Super {
..... ..... }
class Sub extends Super
{ ..... .....
}
Types of inheritance in Java
Based on class, there can be three types of inheritance in java: single, multilevel and
hierarchical. However, Java does not support multiple inheritances with classes. Thus to
achieve multiple inheritances only with the help of Interfaces
In java programming, multiple and hybrid inheritance is supported through interface only
SVKM’s NMIMS University
School of Technology Management & Engineering
COURSE: Object Oriented Programming Through JAVA
1. Single inheritance: When one class inherits another class, it is known as single level
inheritance
2. Multilevel inheritance: is a process of deriving a class from another derived class
3. Hierarchical inheritance: is defined as the process of deriving more than one class from
a base class.
4. Multiple inheritance is defined as the process of deriving a class from more than one
base class.
5. Hybrid inheritance: Hybrid inheritance is a combination of simple, multiple inheritance
and hierarchical inheritance.
Polymorphism
Polymorphism is the ability to present the same interface for differing underlying forms (data
types). With polymorphism, each of these classes will have different underlying data. Precisely,
Poly means ‘many’ and morphism means ‘forms’.
Types of Polymorphism IMP
1. Compile Time Polymorphism (Static)
2. Runtime Polymorphism (Dynamic)
Compile Time Polymorphism: The polymorphism which is implemented at the compile time is
known as compile-time polymorphism. Example - Method Overloading
Method Overloading: Method overloading is a technique, which allows you to have more than
one function with the same function name but with different functionality. Method overloading
can be possible on the following basis:
1. The return type of the overloaded function.
2. The type of the parameters passed to the function.
3. The number of parameters passed to the function.
SVKM’s NMIMS University
School of Technology Management & Engineering
COURSE: Object Oriented Programming Through JAVA
Runtime Polymorphism: Runtime polymorphism is also known as dynamic polymorphism.
Function Overriding is an example of runtime polymorphism.
class Parent {
void myMethod() {
//original implementation
}
}
class Child extends Parent {
@override
void myMethod() {
//new implementation
}
}
Function Overriding means when the child class contains the method, which is already present in
the parent class. Hence, the child class overrides the method of the parent class. In case of
function overriding, parent and child classes both contain the same function with a different
definition. The call to the function is determined at runtime is known as runtime polymorphism.
Task 1:
For the following Problem Statements write programs using classes, objects and methods
1. A class “student” defines student with data members name, roll number and date of birth
and methods as Inputdata(). Inputdata() accepts the input for instance variable. Derived
the class “marks” from class “student” defines marks of various subjects, total percentage
and grade of the student and methods as readdata() reads the marks out of 100, compute()
calculates the percentage and assigns the grades accordingly, show() displays the name,
roll number and date of birth, marks in various subjects, total, percentage and grade.
2 Create a class “Person” with fields “name” and “age” and a method “display()” that prints
the name and age of the person.
Create a subclass “Employee” that extends “Person” and adds a field “salary” and a
method “display_Emp()” that prints the name, age, and salary of the employee.
Create a subclass “Manager” that extends “Person” and adds a field “department” and a
method “display_Manager()” that prints the name, age, salary, and department of the
manager.
Create an object of the “Employee” class and call the “display () and display_Emp()”
SVKM’s NMIMS University
School of Technology Management & Engineering
COURSE: Object Oriented Programming Through JAVA
method.
Create an object of the “Manager” class and call the “display () and display_Manager ()”
method.
3 Create a class “Vehicle” with a method “drive ()” that displays a message “Driving a
vehicle”. Create two subclasses “Car” and “Bike” that extend “Vehicle” and implement
the “drive_car ()” and “drive_bike ()” method to display “Driving a car” and “Driving a
bike” respectively.
4 Create a class “Person” with a method “speak ()” that displays a message “The person is
speaking”. Create two subclasses “Student” and “Teacher” that extend “Person” and
perform the “speak ()” method to display “The student is asking a question” and “The
teacher is giving a lecture” respectively.
5 The program now uses a "Flight" base class and its subclasses "PassengerFlight" and
"CargoFlight." Each subclass overrides the “fly()” method to provide specific behavior.
6 Write a Java program to create an abstract class Shape3D with abstract methods
calculateVolume() and calculateSurfaceArea(). Create subclasses Sphere and Cube that
extend the Shape3D class and implement the respective methods to calculate the volume
and surface area of each shape.
7 Write a Java program to create an abstract class Instrument with abstract methods play()
and tune(). Create subclasses for Glockenspiel and Violin that extend the Instrument class
and implement the respective methods to play and tune each instrument.
8 Create an online shopping system where different product categories are managed using
multi-level inheritance. The base class, Product, should contain attributes like
productName and price. The Electronics class should extend Product, adding attributes
such as warrantyPeriod and a method showWarrantyDetails(). Further, the Laptop class
should inherit from Electronics and introduce a batteryBackup attribute and a method
displayLaptopSpecifications(). The system should show how a Laptop object can utilize
functionalities from both Electronics and Product.
SVKM’s NMIMS University
School of Technology Management & Engineering
COURSE: Object Oriented Programming Through JAVA
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
Students must submit the soft copy as per following segments within two hours of the practical.
The soft copy must be uploaded on the portal at the end of the practical. The filename should be
JAVA_batch_rollno_experimentno Example: JAVA_A1_A001_P1
Roll No.: Name:
Prog/Yr/Sem: Batch:
Date of Experiment: Date of Submission:
Task 1: Program with output
1.
2.
Conclusion (Learning Outcomes): Reflect on the questions answered by you jot down your
learnings about the Topic:
Homework Questions:
1. Examine the output of below given snippet code and write your observations or comments.
class D{
protected int x = 1;
protected void setX(int a){ x=a; }
protected int getX(){ return x;}
}
class B extends D{
protected int x = 3;
public int getX(){ return x; }
public int getB(){ return x; }
}
public class C {
public static void main(String[] args) {
// TODO Auto-generated method stub
D a = new D();
B b = new B();
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link](a.x);
[Link](b.x);
}
SVKM’s NMIMS University
School of Technology Management & Engineering
COURSE: Object Oriented Programming Through JAVA
2. Examine the output of below given snippet code and write your observations or comments.
class X
{
public X() { [Link]("Class A Constructor"); }
}
class y extends X
{
public y() { [Link]("Class B Constructor"); }
}
class z extends X
{
public z() { [Link]("Class C Constructor"); }
}
public class MainClass {
public static void main(String[] args) {
z a=new z();
y b=new y();
}
3. Demonstrate the method override and overloading concepts in hierarchical inheritance.