CS计算机代考程序代写 Java OOP_inJava_19T3

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

Method Forwarding

v Suppose class C extends class A, and also implements interface X.
v As all the methods defined in interface X are abstract, class C needs to implement all

these methods.
v However, there are three implementations of X (in P,Q,R).
v In class C, we may want to use one of these implementations, that means, we may

want to use some or all methods implemented in P, Q or R.
v Say, we want to use methods implemented in P. We can do this by creating an object

of type class P in class C, and through this object access all the methods implemented
in P.

v Note that, in class C, we do need to provide required stubs for all the methods in the
interface X. In the body of the methods we may simply call methods of class P via the
object of class P.

v This approach is also known as method forwarding.
COMP2511: OOP in Java 30

Methods Overriding (Polymorphism)

v When a class defines a method using the same name, return type,
and by the number, type, and position of its arguments as a method in its superclass,
the method in the class overrides the method in the superclass.

v If a method is invoked for an object of the class, it’s the new definition of the method
that is called, and not the superclass’s old definition.

Polymorphism
• An object’s ability to decide what method to apply to itself, depending on where
it is in the inheritance hierarchy, is usually called polymorphism.

COMP2511: OOP in Java 31

Methods Overriding: Example
In the example below,

v if p is an instance of class B,
p.f() refers to f() in class B.

v However, if p is an instance of class A,
p.f() refers to f() in class A.

The example also shows how to refer to the overridden method using super keyword.

COMP2511: OOP in Java 32

Methods Overriding: Example
Suppose class C is a subclass of class B, and class B is a subclass of class A.

Class A and class C both define method f().

From class C, we can refer to the overridden method by,

super.f()

This is because class B inherits method f() from class A.

However,

v if all the three classes define f(), then
calling super.f() in class C invokes class B’s definition of the method.

v Importantly, in this case, there is no way to invoke A.f() from within class C.

v Note that super.super.f() is NOT legal Java syntax.
COMP2511: OOP in Java 33

Method Overloading
Defining methods with the same name and different argument or return types is called
method overloading.

In Java,
v a method is distinguished by its method signature – its name, return type, and by the

number, type, and position of its arguments

For example,
double add(int, int)
double add(int, double)
double add(float, int)
double add(int, int, int)
double add(int, double, int)

COMP2511: OOP in Java 34

Data Hiding and Encapsulation
We can hide the data within the class and make it available only through the methods.

This can help in maintaining the consistency of the data for an object, that means the state
of an object.

Visibility Modifiers
Java provides five access modifiers (for variables/methods/classes),

v public – visible to the world
v private – visible to the class only
v protected – visible to the package and all subclasses
v No modifier (default) – visible to the package

COMP2511: OOP in Java 35

Constructors
v Good practice to define the required constructors for all classes.
v If a constructor is not defined in a class,

o no-argument constructor is implicitly inserted.
o this no-argument constructor invokes the superclass’s no-argument constructor.
o if the parent class (superclass) doesn’t have a visible constructor with no-argument,

it results in a compilation error.
v If the first statement in a constructor is not a call to super() or this(),

a call to super () is implicitly inserted.
v If a constructor is defined with one or more arguments,

no-argument constructor is not inserted in that class.
v A class can have multiple constructors, with different signatures.
v The word “this” can be used to call another constructor in the same class.

COMP2511: OOP in Java 36

Diamond Inheritance Problem: A Possible Solution

COMP2511: OOP in Java 37

Some References to Java Tutorials

v https://docs.oracle.com/javase/tutorial/

v https://www.w3schools.com/java/default.asp

v https://www.tutorialspoint.com/java/index.htm

COMP2511: OOP in Java 38