SSN College of Engineering
Department of Computer Science and Engineering
II Year CSE (III Semester)
Academic Year 2024-25
UCS2313 – Object Oriented Programming Lab
Exercise 4: Abstract Classes and Interfaces
Objective:
● To test the following Inheritance type: multiple inheritance.
● To test the Polymorphism through Interface / abstract classes by method overriding.
Learning Outcome:
● Need of interface and its implementation in Java
● Need of abstract class and its implementation in Java
● Multiple inheritance
● Accessing the derived class objects through base class/interface reference – Dynamic
method dispatch/Dynamic binding
Best Practices:
● Class Diagram usage
● Naming convention – for file names, variables
● Comment usage at proper places
● Prompt messages during reading input and displaying output
● Incremental program development
● Modularity
● All possible test cases in output
1. Create a class hierarchy for the classes defined below. The notations to denote the
access specifiers for the attributes and methods are as follows:
- Private members
+ Public members
# Protected members
~ Default (Package private)
Class Person:
Person
-name:String
-address:String
+Person(name,address)
+getName():String
+getAddress():String
+setAddress(address):void
A sub-class Employee of class Person:
Employee
-empid:String
-dept:String
-basic:int
+Employee(name,address,empid,dept,basic)
+getEmpid():int
+getDept():String
+setDept(dept):void
+getBasic():int
+setBasic(basic):void
+abs calSalary():float
A sub-class Faculty of class Employee:
Faculty
-designation:String
-course:String
+Faculty(name,address,empid,dept,basic,desig,course)
+getDesig():String
+setDesig(desig):void
+getCourse():float
+setCourse(course):void
+calSalary():float
An interface Student:
<<Student>>
+getMarks():float []
+calcGPA():float
A sub-class Teaching Assistant of class Employee that implements interface Student:
TeachingAssistant
-project:String
-course:String
-marks:float []
+TeachingAssistant(name,address,empid,dept,basic,project,course,marks)
+getProject():String
+getCourse():String
+setCourse(course):void
+getMarks():float []
+calcGPA():float
+calSalary():float
Things to do:
1. Draw the class diagram of the above class hierarchy.
2. Implement the above class hierarchy by using Interface and Abstract class.
Hint:
To write an Interface:
● Only abstract methods can be declared inside the Interface.
● Identify the common behavior of the set of objects and declare that as abstract
methods inside the Interface.
● The classes that implement the Interface will provide the actual implementation of
those abstract methods.
To write an Abstract class:
1. An abstract class can have constructor(s), abstract or non-abstract method(s).
2. Define the constructors and non-abstract methods in the Abstract class Shape.
Declare the common behavior as the abstract method.
3. Let the classes define its own constructors, member variables and methods.
4. Write a test driver function to get inputs for Faculty and Teaching Assistant and
display their details.
5. Find the class that can be declared as abstract.
6. Note down the differences while implementing the Inheritance through Interface and
Abstract class.
7. Note the run-time polymorphism in resolving the method call exhibited by Java
through method overriding.
------------------------------------------------------------------------------------------------------
2. Design a Java interface for ADT Stack with the following methods.
Maximum size of stack=15
boolean isFull()
boolean isEmpty()
void push(int element)
int pop()
int peep()
Implement this interface using a class with an integer array to store the elements to
perform the following stack operations.
Boolean balanceParanthesis(String expression)
boolean checkTwoStacks(Stack s1, Stack s2)
--------------------------------------------------------------------------------------------------------
Additional Question:
1. Create an interface named “Vehicle” which consists of the following methods
a. getName()
b. setName(String)
c. getMaxPassengers()
d. setMaxPassengers(int)
e. getMaxSpeed()
f. setMaxSpeed(int)
Inherit an interface named “LandVehicle” from “Vehicle” which contains the following
methods
a) getNumWheels()
b) setNumWheels(int)
c) drive()
Inherit an interface named “SeaVehicle” from “Vehicle”which contains the following
methods
a) getDisplacement()
b) setDisplacement(int)
c) launch()
• Create a class “Jeep” inherits from “LandVehicle” which has soundHorn() method.
• Create a class “Frigate” inherits from “SeaVehicle”which has fireGun() method.
• Create a class “HoverCraft” inherits from both “LandVehicle”and “SeaVehicle”
which has enterLand() and enterSea() methods.
• Create an Interface “EmergencyVehicle” which contains soundSiren() method.
• Create a class “Ambulance” inherits from “LandVehicle” and “EmergencyVehicle”
which has patientIn() method.
• Draw the class diagram which shows the above problem and implement it.