OOP_inJava_19T3
COMP2511
Object Oriented Programming (OOP)
in Java
Prepared by
Dr. Ashesh Mahidadia
OOP in Java
v Object Oriented Programming (OOP)
v Inheritance in OOP
v Introduction to Classes and Objects
v Subclasses and Inheritance
v Abstract Classes
v Single Inheritance versus Multiple Inheritance
v Interfaces
v Method Forwarding (Has-a relationship)
v Method Overriding (Polymorphism)
v Method Overloading
v Constructors
COMP2511: OOP in Java 2
Ashesh Mahidadia
Subclasses and Inheritance:
First Approach
We want to implement GraphicalCircle.
This can be achieved in at least 3 different ways.
First Approach:
v In this approach we are creating the
new separate class for GraphicalCircle and
re-writing the code already available in the class
Circle.
v For example, we re-write the methods area and
circumference.
v Hence, this approach is NOT elegant, in fact its
the worst possible solution.
Note again, its the worst possible solution!
COMP2511: OOP in Java 14
Subclasses and Inheritance:
Second Approach
v We want to implement GraphicalCircle so
that it can make use of the code in the class
Circle.
v This approach uses “has-a” relationship.
v That means, a GraphicalCircle has a
(mathematical) Circle.
v It uses methods from the class Circle (area
and circumference) to define some of the
new methods.
v This technique is also known as method
forwarding.
COMP2511: OOP in Java
15
Subclasses and Inheritance:
Third Approach – Extending a Class
v We can say that GraphicalCircle is-a Circle.
v Hence, we can define GraphicalCircle as an
extension, or subclass of Circle.
v The subclass GraphicalCircle inherits all the
variables and methods of its superclass Circle.
COMP2511: OOP in Java
16
Subclasses and Inheritance: Example
We can assign an instance of GraphicCircle to a Circle variable. For example,
GraphicCircle gc = new GraphicCircle();
…
double area = gc.area();
…
Circle c = gc;
// we cannot call draw method for “c”.
Important:
v Considering the variable “c” is of type Circle,
v we can only access attributes and methods available in the class Circle.
v we cannot call drawmethod for “c”.
COMP2511: OOP in Java
17
Super classes, Objects, and the Class Hierarchy
v Every class has a superclass.
v If we don’t define the superclass, by default, the superclass is the class Object.
Object Class :
v Its the only class that does not have a superclass.
v The methods defined by Object can be called by any Java object (instance).
v Often we need to override the following methods:
• toString()
o read the API at https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#toString()
• equals()
o read the API at
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)
• hasCode()
COMP2511: OOP in Java 18
Abstract Classes
Using abstract classes,
v we can declare classes that define only part of an implementation,
v leaving extended classes to provide specific implementation of some or all the
methods.
The benefit of an abstract class
v is that methods may be declared such that the programmer knows the interface
definition of an object,
v however, methods can be implemented differently in different subclasses of the
abstract class.
COMP2511: OOP in Java 19
Abstract Classes
Some rules about abstract classes:
v An abstract class is a class that is declared abstract.
v If a class includes abstract methods, then the class itself must be declared abstract.
v An abstract class cannot be instantiated.
v A subclass of an abstract class can be instantiated if it overrides each of the abstract
methods of its superclass and provides an implementation for all of them.
v If a subclass of an abstract class does not implement all the abstract methods it
inherits, that subclass is itself abstract.
COMP2511: OOP in Java 20
Abstract Class: Example
COMP2511: OOP in Java 21
Abstract Class: Example
COMP2511: OOP in Java 22
Abstract Class: Example
COMP2511: OOP in Java 23
We can now write code like this:
Some points to note:
v As Shape is an abstract class, we cannot
instantiate it.
v Instantiations of Circle and Rectangle can be
assigned to variables of Shape.
No cast is necessary
v In other words, subclasses of Shape can be
assigned to elements of an array of Shape.
No cast is necessary.
v We can invoke area() and circumference()
methods for Shape objects.
Single Inheritance versus Multiple Inheritance
• In Java, a new class can extend exactly one superclass – a
model known as single inheritance.
• Some object-oriented languages employ multiple
inheritance, where a new class can have two or more super
classes.
• In multiple inheritance, problems arise when a superclass’s
behaviour is inherited in two/multiple ways.
• Single inheritance precludes some useful and correct
designs.
• In Java, interface in the class hierarchy can be used to add
multiple inheritance, more discussions on this later.
COMP2511: OOP in Java 24
Diamond inheritance
problem