Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.

Similar presentations


Presentation on theme: "COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming."— Presentation transcript:

1 COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming

2 There are four basic properties discussed under Object Oriented Programming. 1. Encapsulation. 2. Inheritance. 3. Polymorphism. 4. Abstraction 2 Introduction

3 Encapsulation is the process of combining state and behavior into a self-contained unit. It can be considered as a protective wrapper that prevents the methods and variables from being arbitrarily accessed by other code defined outside the wrapper. The basis of encapsulation is the class because a class is a representation of common state and behavior. Then the access modifiers (public, private, protected, default) are used to control the scope that the data and the code in a class can be accessed. 3 Encapsulation

4 4 Encapsulation…

5 public class Employee { private int empNo; private String name; public void setEmpNo(int empNo) { this.empNo=empNo; } public int getEmpNo() { return this.empNo; } 5

6 Encapsulation… public void setName(String name) { this.name=name; } public String getName() { return this.name; } 6

7 Encapsulation… Data Hiding. Once the attributes of the Employee class are declared as private there is no possibility for other classes or programs to access these data directly. Therefore each data item is provided with two public methods each for setting and getting the values of the variables(setters and getters). These methods are the ones which act as the interface for the programs outside the Employee class. This insulation of the data (attributes) from direct access by the programs outside the class is called data hiding. 7

8 Inheritance Inheritance is the process by which one object acquires the properties of another object. This is a mechanism that facilitates the definition of a new class(classes) by using an existing class(classes). 8 name age setName() getName() setAge() getAge() PersonEmployee Manager name age empNo setName() getName() setAge() getAge() setEmpNo() getEmpNo() name age empNo mngCode setName() getName() setAge() getAge() setEmpNo() getEmpNo() setMngCode() getMngCode()

9 Inheritance… Here, the classes Person and Employee have the attribute name and age in common. Therefore it is more convenient if it is possible to reuse the data in the Person class to create the Employee class. This can be done using the inheritance property of object oriented programming. That is the new class Employee can be derived from the existing class Person. Then the existing class is know as the super class where as the new class is called the sub class. 9

10 Inheritance… This is graphically denoted as follows 10 Generalization and Specialization In the above example the Person class contains the common attributes and behaviors of a person and an employee. This is a general representation of both these entities. Therefore this is know as the generalization. Then the Employee class contains the attributes and methods defined in the Person class and some more features which are specific for the employee. That is an Employee is a special case of Person class. Therefore this Employee class is know as the Specialization.

11 Inheritance… Similarly, for the Manager class the attributes and methods can be derived from the Employee class. Syntax The derivation of the class from another class should be specified at the class declaration. class class_name extends super_class_name 11

12 Inheritance… Different form of inheritance Single inheritanceMultilevel inheritance 12 Hierarchical inheritance

13 Inheritance… Different form of inheritance 13 Multiple inheritance Out of these types of inheritance Java does not support multiple inheritance

14 Inheritance… Use of Super To call super class constructor To call super class member Call super class constructor A subclass can call a constructor method defined by its superclass by use of the following form of super: super(parameter-list); Here, parameter-list specifies any parameters needed by the constructor in the superclass. super() must always be the first statement executed inside a subclass constructor. 14

15 Inheritance… Call super class member Syntax: super. member Here, member can be either a method or an instance variable. Ex: display method of the Employee class can be modified using this method. 15

16 Polymorphism polymorphism refers to the fact that a single operation can have different behaviors in different objects. The word polymorphism is a combination of two Greek words. poly + morphism There are two ways to implementing polymorphism. 1. Overloading Methods. Methods have the Same method name but method signature and the coding(method body) are different in sub classes. 16

17 Polymorphism… 2. Overriding Methods. Methods have the same method signature only the coding is different in subclasses. Dynamic Binding Also known as Late binding or Virtual binding. It is a situation where compiler is unable to resolve the procedure call and the binding is done at runtime. Static Binding Also known as Early binding. The compiler can resolve the binding at compile time 17

18 Polymorphism… All the instance method calls are always resolved at runtime, but all the static method calls are resolved at compile time itself and hence we have static binding for static method calls. Because static methods are class methods and hence they can be accessed using the class name itself and therefore access to them is required to be resolved during compile time only using the compile time type information. 18

19 Polymorphism… 19

20 Polymorphism… 20 public class TestPolymorphism { public static void main(String args[]) { Person p; Employee e1=new Employee("Nimal",23,"Emp001"); Student s1=new Student("Amila",24,“uwu/09/0001"); p=e1; p.display(); p=s1; p.display(); } Name:Nimal Age :23 Emp No :Emp001 Name:Amila Age :24 Reg No :uwu/09/0001 Dynamic Bynding

21 Polymorphism… Here, according to the object (Employee object or Student object) assigned to the Person reference p different display methods are executed. note that; Polymorphism = Inheritance + Dynamic binding. 21

22 Abstraction 22 Abstraction is the act of representing essential features without giving background details or explanations. Can be achieved two ways 1. Abstract classes 2. Interfaces

23 Abstraction… 23 Abstract classes A class which can not be instantiated. It is a class that do not have objects (can not create objects). Syntax: abstract class identifer { } Abstract classes can be sub classed. That is new classes can be created by extending an abstract class. May or may not contain abstract method.

24 Abstraction… 24 Abstract Method Is a method that is declared without an implementation. That is an abstract method does not have a method body. Syntax: abstract ( ); If there is an abstract method in a class that class should be declared as abstract. If an abstract class is extended by a new class, the derived class should implement all the abstract methods in the base class. Otherwise the derived class should also be declared as abstract.

25 Abstraction… 25 This property can be used to force the implementation of some methods in a derived class of a particular base class. That is, if there is a method that a sub class should have, that method can be included into the super class as an abstract method.

26 Abstraction… 26

27 Abstraction… 27 consider the deposit() and withdraw() methods in the above three classes. Assume that a kid can withdraw maximum of 500.00 rupees in one transaction. The deposit() method is same for both SavingsAccount and KidsAccount. But for the withdraw() method there are some restrictions for the KidsAccount.

28 Abstraction… 28 public void withdraw(double amount) { if(amount<balance) balance-=amount; else System.out.println("Error:"); } public void withdraw(double amount) { if(amount<balance) if(amount<=500) balance-=amount; else System.out.println("Amount greaterthan 500.00"); else System.out.println("Error:"); } Withdraw method of Savings AccountWithdraw method of Kids Account

29 Abstraction… 29 Because of this difference in implementation of the withdraw method it is useful to implement in the base class. Therefore can define the withdraw method as an abstract method in the Account class so that the derived classes can implement is as necessary.

30 Abstraction… 30 abstract class Account { private String accNo; private String name; private double balance; public void deposit(double amount) { balance+=amount; } //Since the two sub classes have different withdraw method it can be declared as //an abstract method in the base class. public abstract void withdraw(double amount); }

31 Interface 31 Interface An interface is a collection of methods that can be used to implement varying behaviors. It provides only a form, but no implementation. Declaring interfaces There are no attributes in an interface. May contain constants and methods. If attributes are created, they are implicitly static and final. Methods are public. Syntax: interface keyword is used to implement an interface. interface identifer { //constants if any //method signatures }

32 Interface… 32 Using Interfaces If an interface is used to create a new class, that new class should implement all the methods in the interface. Keyword implements is used. Syntax: class identifier implements interface_name1,interface_name2,... { //attributes //implementation of all methods in the interface. }

33 Interface… 33 e.g. interface Loan{ double intRate=10.5; //constants can be defined here double calIntesrest() //methods in the interface void loanDetails() } class HouseLoan implements Loan {String houseLoanNum;//attributes of HouseLoan double calInterest() { //implements method } void loanDetails() { //implements method }

34 Interface… 34 E.g. Circle and Cube are two different shapes. Write a java program to calculate surface area and volume of these two shapes based on user inputs. (Hint: use interfacing)


Download ppt "COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming."

Similar presentations


Ads by Google