Content Analysis
Object Oriented Design and Programming
Week 10
A B Emran Salahuddin (Sydney)
Manzur Ashraf (Melbourne)
Kent Institute Australia Pty. Ltd.
ABN 49 003 577 302 CRICOS Code: 00161E RTO Code: 90458 TEQSA Provider Number: PRV12051
Version 2 – 18th December 2015
1
SLIDE TITLE
Farrell, J. (2017) Programming Logic and Design, Comprehensive (9th ed.) Cengage Learning
2
2
Programming Logic and Design Ninth Edition
Principles of Object Oriented Design and programming – Encapsulation, Polymorphism and Inheritance
3
Encapsulation
4
Encapsulation in Java is a process of wrapping code and data together into a single unit.
We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
For example, a capsule which is mixed of several medicines.
4
Encapsulation
5
By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.
It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.
5
Encapsulation
6
It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.
The encapsulate class is easy to test. So, it is better for unit testing.
The standard IDE's are providing the facility to generate the getters and setters. So, it is easy and fast to create an encapsulated class in Java.
6
Encapsulation Example
7
Programming Logic and Design, Ninth Edition
Encapsulation Example
8
8
Inheritance
9
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviours of a parent object.
It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes.
9
Inheritance
10
When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship
10
Inheritance
11
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.
11
Inheritance Example
12
Programmer is the subclass and Employee is the superclass. The relationship between the two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee.
12
Inheritance
13
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
13
Inheritance
Super keyword
The super keyword refers to the objects of immediate parent class.
Used to access the data members of parent class when both parent and child class have member with same name. super.variablename
Used to explicitly call the no-arg and parameterized constructor of parent class. super() and super(parameterlist)
Used to access the method of parent class when child class has overridden that method. super.method()
14
Inheritance
15
Why use inheritance in java?
For Method Overriding.
For Code Reusability.
15
Method Overriding
16
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
16
Method Overriding
17
Rules for Java Method Overriding
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance).
17
Method Overriding
18
18
Method Overriding
19
toString() method
Every class in Java is a child of the Object class either directly or indirectly. And since the Object class contains a toString() method, we can call toString() on any instance and get its string representation.
So we are overriding the toString() method of object class.
If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
19
Method Overriding
20
toString() method
20
Polymorphism
21
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms.
So polymorphism means many forms.
Polymorphism is the capability of a method to do different things based on the object that it is acting upon.
21
Polymorphism
22
There are two types of polymorphism in Java:
Runtime polymorphism
Compile-time polymorphism
22
Runtime Polymorphism
23
Runtime polymorphism is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a superclass. Already done in previous slides.
The determination of the method to be called is based on the object being referred to by the reference variable.
23
Runtime Polymorphism
24
Splendor class extends Bike class.
We are calling the run method by the reference variable of Parent class.
Since it refers to the subclass object and subclass method overrides the Parent class method, the subclass method is invoked at runtime.
Since method invocation is determined by the JVM not compiler, it is known as runtime polymorphism.) method.
Another example
25
Another Example (continued)
26
Compile Time Polymorphism
27
Method overloading is an example of compile time polymorphism.
Method Overloading: This allows us to have more than one method having the same name, if the parameters of methods are different in number, sequence and data types of parameters.
27
Method Overloading
28
28
Summary
Encapsulation, inheritance and polymorphism are three main principles of Object Oriented Programming.
There are different types of inheritance.
Method overloading and method overriding are types of polymorphism.
29
kent.edu.au Kent Institute Australia Pty. Ltd. ABN 49 003 577 302 ● CRICOS Code: 00161E ● RTO Code: 90458 ● TEQSA Provider Number: PRV12051
30
30