Abstract Class and Polymorphism
Abstract Class and Polymorphism
The ArrayList class implements a grow-able List of objects. It is better to use ArrayList class, rather than
creating your own linked-list structure.
ArrayList Iterator:
Used to loop through all the elements of ArrayList (Alternative of for and for-each loop to traverse the
ArrayList)
Example code
import java.util.ArrayList;
import java.util.Iterator;
stringArrayList.add("Adding");
stringArrayList.add("String");
stringArrayList.add(1,"a");
} }
Output
Centre for Advanced Studies in Engineering
Course Code: CS221L Object Oriented Programming
Lab 08: Abstract Classes and Polymorphism
1. Adding
2. a
3. String
Abstract Classes:
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract
classes cannot be instantiated, but they can be sub-classed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a
semicolon), like this:
If a class includes abstract methods, then the class itself must be declared abstract, as in:
When an abstract class is sub-classed, the subclass usually provides implementations for all of the abstract
methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
Example
Polymorphism:
Polymorphism is a generic term that means 'many shapes'. More precisely Polymorphism means the ability to
request the same operations be performed by a wide range of different types of things. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Task 1:
In this task you are going to implement the concept of polymorphism.
Create a class named Person
o Person Class should be abstract.
o A getName() method that is abstract as well.
Create a class named Student
o Student inherits the Person Class.
o Override getName() method.
Create a class named Professor
o Professor inherits the Person Class.
o Override getName() method.
Person
getName():String
Student Professor
+getName():String +getName():String
TestClass
+ printName(person : Person)
+ main(args : String [])
The name person in the printName(Person) method may denote instances of different classes (namely
of Student and of Professor). This works since both inherit from the Person class which defines
the getName() method. The compiler makes sure that all subclasses have a getName() method.
Task 2:
Payroll System using Polymorphism:
A company pays its employees monthly. The employees are of two types:
A part time employee, working on hourly basis.
A full time employee, a permanent employee having fixed salary per month.
Employee
Now you have to create the following classes in order to capture the above scenario.
Employee class:
1. Make this class abstract.
2. Data Members are: Name, ID, Gender, and Salary, declare Salary as protected.
3. Default constructor.
4. Overloaded constructor with arguments Name, ID, Gender.
5. Implement toString() method that returns a string of all data members.
6. Write a calculateSalary() function and make it abstract.
1. The Visiting Employee class has additional data members WorkingHours, PayRate.
2. Default Constructor.
3. Overloaded constructor with arguments Name, ID, Gender, PayRate, WorkingHour and call to the
Employee constructor using super keyword.
4. Getters and Setters.
5. Implement toString() method that returns a string of all data members of superclass and subclass.
6. Override calculateSalary() method (which was declared abstract in Employee Class) that will set the
Salary on the basis of hourly system. e.g. if pay rate is 50 and working hours are 20 then salary will be
(20*50).
Main Function: