Lecture 01 - Review of OO Concepts
Lecture 01 - Review of OO Concepts
Lecture # 01:
Review of
Object-Oriented Concepts
in JAVA
Dr. Ahmed Abba Haruna
Assistant Professor,
University of Hafr Al-Batin (UoHB)
College of Computer Science and Engineering (CCSE)
Hafr Al-Batin 31991, Saudi Arabia.
[email protected]
© Slides adapted from the textbook, ICS 202 slides from KFUPM and/or CS 211 slides from Taibah University (Ms. Rana & Mr. Modhi) and/or many other online resources.
Review of Object-Oriented Concepts in JAVA
• Introduction
• Advantages of Object-Orientation.
• Inheritance.
• Abstract Classes.
• Interfaces.
• Review Questions.
2
Introduction
• Data Structures: A data structure is a collection of data
values, the relationships among them, and the functions
or operations that can be applied to the data. In simple
words, it is a particular way of organizing data in a
computer so that it can be used effectively. For
example, we can store a list of items having the same
data-type using the array data structure.
• Computer Science is the study of data structures and
algorithms.
• Object Oriented Programming (OOP): Four major
principles of an object oriented language are
Encapsulation, Data Abstraction, Polymorphism and
3 Inheritance.
Cont…
• Class: A class is a blueprint or prototype from which
objects are created. A class models the state and
behavior of a real-world object.
• Object: An object is a software bundle of related state
and behavior. It is a specimen / instance of class.
• Package: A package is a namespace for organizing
classes and interfaces in a logical manner. Packages
make large software projects easier to manage.
• Interface: An interface is a contract between a class
and the outside world. When a class implements an
interface, it promises to provide the behavior published
by that interface.
4
Object-Oriented Concepts supported by JAVA
• Java provides explicit support for many of the fundamental Object-
Oriented Concepts. Some of these are:
class Employee {
protected String name;
protected double payRate;
public Employee(String name, double payRate) {
this.name = name;
this.payRate = payRate;
}
public String getName() {return name;}
public void setPayRate(double newRate) { payRate = newRate; }
8
Notes about Inheritance
We observe the following from the examples on inheritance:
• Methods and instance variables of the super class are inherited by
subclasses, thus allowing for code reuse.
• A subclass can define additional instance variables (e.g. hours)
and additional methods (e.g. addHours).
• A subclass can override some of the methods of the super
class to make them behave differently (e.g. the pay & print)
• Constructors are not inherited, but can be called using the super
keyword. Such a call must be the first statement.
• If the constructor of the super class is not called, then the
complier inserts a call to the default constructor -watch out!
• super may also be used to call a method of the super class.
9
Review of Abstract Classes
• Inheritance enforces hierarchical organization, the benefits of
which are: reusability, type sharing and polymorphism.
• Java uses Abstract classes & Interfaces to further strengthen the
idea of inheritance.
• To see the role of abstract classes, suppose that the pay method is
not implemented in the HourlyEmployee subclass.
– Obviously, the pay method in the Employee class will be
assumed, which will lead to wrong result.
– One solution is to remove the pay method out and put it in
another extension of the Employee class, MonthlyEmployee.
– The problem with this solution is that it does not force
subclasses of Employee class to implement the pay method.
10
Review of Abstract Classes (Cont'd)
The solution is to declare the pay method of the Employee class as
abstract, thus, making the class abstract.
12
Review of Abstract Classes (Cont'd)
class Executive extends MonthlyEmployee {
private double bonus;
public Executive(String exName, double exRate) {
super(exName, exRate);
bonus = 0;
}
public void awardBonus(double amount) {
bonus = amount;
}
public double pay() {
double paycheck = super.pay() + bonus;
bonus = 0;
return paycheck;
}
public void print() {
super.print();
System.out.println("Current bonus: " + bonus);
}
}
HourlyEmployee
Employee
MonthlyEmployee Executive
13
Review of Abstract Classes (Cont'd)
• The following further illustrates the advantages of organizing
classes using inheritance - same type, polymorphism, etc.
public class TestAbstractClass {
public static void main(String[] args) {
Employee[] list = new Employee[3];
list[0] = new Executive("Jarallah Al-Ghamdi", 50000);
list[1] = new HourlyEmployee("Azmat Ansari", 120);
list[2] = new MonthlyEmployee("Sahalu Junaidu", 9000);
((Executive)list[0]).awardBonus(11000);
for(int i = 0; i < list.length; i++)
if(list[i] instanceof HourlyEmployee)
((HourlyEmployee)list[i]).addHours(60);
for(int i = 0; i < list.length; i++) {
list[i].print();
System.out.println("Paid: " + list[i].pay());
System.out.println("*************************");
}
}
}
14
Review of Interfaces
15
Review of Interfaces (contd.)
• Recall that Java has the Comparable interface already defined as:
interface Comparable {
int compareTo(Object o);
}
• Recall also that java has the java.util.Arrays class, which has a sort
method that can sort any array whose contents are either primitive
values or Comparable objects.
• Thus, to sort our list of Employee objects, all we need is to modify the
Employee class to implement the Comparable interface.
• Notice that this will work even if the Employee class is extending
another class or implementing another interface.
import java.util.Arrays;
public class TestInterface {
public static void main(String[] args) {
Employee[] list = new Employee[3];
list[0] = new Executive("Jarallah Al-Ghamdi", 50000);
list[1] = new HourlyEmployee("Azmat Ansari", 120);
list[2] = new MonthlyEmployee("Sahalu Junaidu", 9000);
((Executive)list[0]).awardBonus(11000);
for(int i = 0; i < list.length; i++)
if(list[i] instanceof HourlyEmployee)
((HourlyEmployee)list[i]).addHours(60);
Arrays.sort(list);
for(int i = 0; i < list.length; i++) {
list[i].print();
System.out.println("Paid: " + list[i].pay());
System.out.println("**********************");
}
}
}
19