You are on page 1of 9

UNIVERSITY OF IRINGA

FACULTY OF SCIENCE AND EDUCATION


DEPARTMENT OF INFORMATION TECHNOLOGY

LECTURE FIVE
POLYMOPHISM, ABSTRATION,ENCAPSULATION,INHERITANCE,
PACKAGE AND AGGREGATION

Reg, No. Course Year


DIT 206 Introduction to OOP 2018/ 2019

Ass. Lecturer: Thobius Joseph (M.Sc. In Telecom Eng.)


Date: Jan, 2019

10/31/2019 Thobius Joseph(Msc in TE Eng.)0783758724 1


CONTENTS
1. Polymorphism
2. Inheritance
3. Encapsulation
4. Abstraction
5. Packages
6. Aggregation
7. Enumeration
8. Interface

10/31/2019 Thobius Joseph(Msc in TE Eng.)0783758724 2


Required Knowledge

Basic Knowledge of C Programming and C++ will


help you to understand Java Programming
quickly, and If you don't know programming and
you are studying Java, so it's quite complicated.

10/31/2019 Thobius Joseph(Msc in TE Eng.)0783758724 3


Polymorphism
 The word polymorphism means having multiple forms.
 Polymorphism can be achieved in two of the following ways:
• Method Overloading(Compile time Polymorphism) (Static Polymorphism)
• Method Overriding(Run time Polymorphism) (Dynamic Polymorphism.)
 Method Overloading(Compile time Polymorphism)
- A method which is overloaded can contain different access modifiers.
- Overloading method's argument lists might differ in:
• Number of parameters passed
• Data type of actual parameters
• Sequence of data type of actual parameters
 Method Overriding(Run time Polymorphism)
- Occurs between super class and sub class and when the method is not declared final. Rules for method overriding;
1. Argument list: The argument list and data types at the time of overriding method need to be same as that of the method of
the parent class.
2. Access Modifier: The Access Modifier present in the overriding method (method of subclass) cannot be more restrictive
than that of an overridden method of the parent class. The private, static and final methods can't be overridden as they are
local to the class.
Method Overloading Method Overriding
class Mltply { class parent { public void work() { System.out.println("Parent is under
void mul(int a, int b) { System.out.println("Sum of two=" + (a * retirement from work."); } }
b)); } class child extends parent { public void work() { System.out.println("Child has
void mul(int a, int b, int c) { System.out.println("Sum of three=" a job"); System.out.println(" He is doing it well"); }
+ (a * b * c)); } } public static void main(String argu[])
class Polymorphism { public static void main(String args[]) { { child c1 = new child(); c1.work(); } }
Mltply m = new Mltply(); m.mul(6, 10); m.mul(10, 6, 5); } }

10/31/2019 Thobius Joseph(Msc in TE Eng.)0783758724 4


Inheritance
• Inheritance can be defined as the process where one class acquires the properties (methods and fields) of
another. By this technique we can create hierarchy of classes.
• The class which inherits the properties of other is known as subclass (derived class, child class) and the
class whose properties are inherited is known as superclass (base class, parent class).
 How we can create inheritance?
• By using extends keyword. extends is the keyword used to inherit the properties of a class. Following is the
syntax of extends keyword.
Inheritance syntax Inheritance example

class SuperClassName { class Calculation {


..... ..... int z;
} public void addition(int x, int y) {
class SubClassName extends SuperClassName { z = x + y; System.out.println("The sum of the given numbers:"+z);
..... ..... }
} public void Subtraction(int x, int y) {
z = x - y; System.out.println("The difference between the given numbers:"+z); } }
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) { z = x * y; System.out.println("The product of the given
numbers:"+z); }
public static void main(String args[]) {
int a = 20, b = 10; My_Calculation demo = new My_Calculation(); demo.addition(a, b);
demo.Subtraction(a, b); demo.multiplication(a, b); }
}

10/31/2019 Thobius Joseph(Msc in TE Eng.)0783758724 5


Abstraction
 Abstraction (refer to lecture 1)
 abstraction is a process of hiding the implementation details from the user, only the functionality will be provided
to the user. In other words, the user will have the information on what the object does instead of how it does it.
 In Java, abstraction is achieved using Abstract classes and interfaces.
 Abstract Class (class ambayo sub classes zake zinakuwa na assimilated features)
• A class which contains the abstract keyword in its declaration is known as abstract class.
• Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )
But, if a class has at least one abstract method, then the class must be declared abstract.
• If a class is declared abstract, it cannot be instantiated i.e. You can not use its constructor to create object.
• To use an abstract class, you have to inherit it from another class, provide implementations to the abstract
methods in it.
• If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
• Can you classify the followings - Elephant, CD player, Television, Chair, Table,
Tiger; How many classes do you identify here?
 The obvious answer anybody would give is three, i.e. Animal, Furniture and
Electronic items. But how do you come to this conclusion? Well, we grouped
similar items by focusing on the generic characteristics rather than specific
characteristics. This is abstraction.
 Assume that animal class, we know animal must eat but elephant and tiger eat
different, hence animal is abstract class with method eat, will physical
implementatation of eat method will be in tiger class and in elephant class.

10/31/2019 Thobius Joseph(Msc in TE Eng.)0783758724 6


Abstraction
Abstract class example Abstract method example

abstract class cycle { public abstract class Employee {


abstract void work(); private String name; private String address; private int number;
} public abstract double computePay(); // Remainder of class definition }
class HeroCycle extends cycle {
void work() { System.out.println("Selling good"); /* File name : Salary.java */
} public class Salary extends Employee {
public static void main(String argu[]) { private double salary; // Annual salary
cycle o = new HeroCycle(); o.work(); public double computePay() {
System.out.println("Code executed"); } System.out.println("Computing salary pay for " + getName());
} return salary/52; } // Remainder of class definition
}

 Abstract Method
• If you want a class to contain a particular method but you want the actual implementation of that method to be
determined by child classes, you can declare the method in the parent class as an abstract.
• abstract keyword is used to declare the method as abstract.
• You have to place the abstract keyword before the method name in the method declaration.
• An abstract method contains a method signature, but no method body.
• Instead of curly braces, an abstract method will have a semoi colon (;) at the end.
 Declaring a method as abstract has two consequences −
• The class containing it must be declared as abstract.
• Any class inheriting the current class must either override the abstract method or declare itself as abstract.
NB: You have to write the full functionality of this method in another non-abstract class, taking the same method
name. See above example

10/31/2019 Thobius Joseph(Msc in TE Eng.)0783758724 7



Encapsulation
Encapsulation simply means binding object state(fields) and behaviour (methods) together. If you are
creating class, you are doing encapsulation.
 How to implement encapsulation in java:
• 1) Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and
get values of these variables through the methods of the class.
• 2) Have getter and setter methods in the class to set and get the values of the fields.Instance Variables (Non-static Variables)
 Advantages of encapsulation
• User would not be knowing what is going on behind the scene. They would only be knowing that to update a field call set
method and to read a field call get method but what these set and get methods are doing is purely hidden from them.
Class Variables (Static Variables)

class EncapsulationDemo{
private int ssn; private String empName; private int empAge;
public int getEmpSSN () { //Getter and Setter methods
return ssn; }
public String getEmpName () {
return empName; }
public int getEmpAge () {
return empAge; }
public void setEmpAge (int newValue) {
empAge = newValue; }
public void setEmpName (String newValue) {
empName = newValue; }
public void setEmpSSN (int newValue) {
ssn = newValue; }
}
public class EncapsTest{ public static void main(String args[]){
EncapsulationDemo obj = new EncapsulationDemo();
obj.setEmpName("Mario");
obj.setEmpAge(32);
obj.setEmpSSN(112233);
System.out.println("Employee Name: " + obj.getEmpName());
System.out.println("Employee SSN: " + obj.getEmpSSN());
System.out.println("Employee Age: " + obj.getEmpAge());
}}

10/31/2019 Thobius Joseph(Msc in TE Eng.)0783758724 8



Encapsulation
To understand what is encapsulation in detail consider the following bank account class with deposit and
show balance methods
• Suppose a hacker managed to gain access to the code of your bank account. Now, he tries to deposit amount -100 into your
account by two ways, which both will fail.
bank account class Hacker approaches
class Account { 1. Usually, a variable in a class are set as "private" as
private int account_number;
private int account_balance; shown below. It can only be accessed with the
public void show Data() {
methods defined in the class. No other class or object
//code to show data can access them.
}

public void deposit(int a) {


if (a < 0) { 2. he tries to do deposit a
//show error
} else amount -100 by using “
account_balance = account_balance + a; deposit" method.
}
}

this will fail bcoz method


will check validity of data
inserted at if (a<0)

10/31/2019 Thobius Joseph(Msc in TE Eng.)0783758724 9

You might also like