features of object oriented programming lec.4

21
Features of Object Oriented Programming Lec.4

Upload: eve

Post on 16-Mar-2016

35 views

Category:

Documents


4 download

DESCRIPTION

Features of Object Oriented Programming Lec.4. Abstraction and Encapsulation. Computer programs can be very complex, perhaps the most complicated artifact sever created by humans. One way to manage and control this complexity is with abstraction . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Features of Object Oriented Programming Lec.4

Features of Object Oriented

ProgrammingLec.4

Page 2: Features of Object Oriented Programming Lec.4

ABSTRACTION AND ENCAPSULATION• Computer programs can be very

complex, perhaps the most complicated artifact sever created by humans. One way to manage and control this complexity is with abstraction.

• An abstraction of something is a simplified view of it—the abstraction “hides” the unnecessary details and allows us to focus only on the parts of interest to us.

Page 3: Features of Object Oriented Programming Lec.4

Millions of us use cars everyday without understanding the details of how they work.

A program can be designed as a set of interacting abstractions. In Java, these abstractions are captured in classes.

The creator of a class obviously has to

know all the details of the class. But once the class is created, other programmers can use the class without knowing its internal details. They only have to know its interface, just as the driver of a car can use the vehicle without knowing how the engine works.

Page 4: Features of Object Oriented Programming Lec.4

• An ADT consists of some hidden or protected data and a set of methods to perform actions on the data. When we hide data in a class, we say that the data have been encapsulated.

Page 5: Features of Object Oriented Programming Lec.4

• Consider for a moment the class String. A variable of type String can be instantiated, using the String constructors, to reference a String object; this object can invoke many predefined instance methods such as length, concat, replace, toUpperCase, and so on.

• The method of implementing the data type String is hidden from the programmer.

Page 6: Features of Object Oriented Programming Lec.4

• As a programmer, there is no way of inspecting how these operations are carried out since the class will be stored as Java bytecodes. Only the implementers of the classes should have access to the Java source code, to prevent other programmers from changing well-engineered software.

Page 7: Features of Object Oriented Programming Lec.4

REQUIREMENTS FOR CREATING ADT■ The abstraction has created a data

type, for example, String.■ It is possible to declare variables of the

type, for example, String alphabet.■ The type contains a set of instance

methods for the access and manipulation of data of the said type, for example, length.

■ The implementation of the type, behind the scenes, uses whatever data and methods are necessary.

■ Access to the type is through a restricted interface with the implementation details being hidden from the programmer who uses the type.

Page 8: Features of Object Oriented Programming Lec.4

2 .INHERITANCE• Inheritance is the process by

which one class receives the characteristics of another class.

• The initial class is called the base or parent class; in Java this is known as the superclass. The receiving class is called the derived or child class; in Java this is known as the subclass.

Page 9: Features of Object Oriented Programming Lec.4

WHAT ARE THE BENEFITS OF USING INHERITANCE?

Inheritance increases your ability to reuse classes. Software can be extended by reusing previously defined classes and adding new methods to the subclasses.

Inheritance increases the level of abstraction in a program.

Inheritance improves the clarity in the design of classes by allowing the implementation of methods to be postponed in super classes and to appear in subclasses.

Page 10: Features of Object Oriented Programming Lec.4

public class Object{// constructorpublic Object();// public instance methodsPublic boolean equals(Object obj);public String toString();// protected instance methodsprotected void finalize() . . .} 

Page 11: Features of Object Oriented Programming Lec.4
Page 12: Features of Object Oriented Programming Lec.4

AN EXAMPLE OF INHERITANCEpublic class Employee{// constantprotected static float

holidayEntitlement = 20.0;

// instance variablesprotected String

employeeName;protected String

employeeDept;protected int

lengthOfService;

// constructorpublic Employee(String

name, String department, intyearsService)

{

employeeName = name;

employeeDept = department;

lengthOfService = yearsService;

}

Page 13: Features of Object Oriented Programming Lec.4

// instance methodspublic String

getName(){Return

employeeName;}public String

getDepartment(){Return

employeeDept;}Public int

getLengthOfService()

{

Return lengthOfService;

}public float

getHolidays(){Return

holidayEntitlement;

}}

Page 14: Features of Object Oriented Programming Lec.4

• protected variables are safe from access from outside a controlled set of classes, yet can still be easily accessed from within the set, that is, from classes in the same package.

• a package is a group of related classes.

Page 15: Features of Object Oriented Programming Lec.4

• Another class, Technician, may be defined that inherits all the characteristics of the class Employee.

• For example, the statement class Technician extends Employee permits all the variables and methods of the class Employee to be inherited by the class Technician.

Page 16: Features of Object Oriented Programming Lec.4

public class Technician extends Employee{// instance variableprotected float holidays;// constructorpublic Technician(String name, String

department, intyearsService){super(name, department, yearsService);}}

Page 17: Features of Object Oriented Programming Lec.4
Page 18: Features of Object Oriented Programming Lec.4

POLYMORPHISM• Polymorphism is a way of giving a

method one name that is shared up and down an object hierarchy, with each object in the hierarchy implementing the method in a way appropriate to itself

To write polymorphic classes we require two things:

• The classes must be part of the same inheritance hierarchy.

• The classes must support the same set of required methods.

 

Page 19: Features of Object Oriented Programming Lec.4
Page 20: Features of Object Oriented Programming Lec.4

STATIC POLYMORPHISM:

• In Java, static polymorphism is achieved through method overloading. Method overloading means there are several methods present in a class having the same name but different types/order/number of parameters.

• At compile time, Java knows which method to invoke by checking the method signatures.  So, this is called compile time polymorphism or static binding.

Page 21: Features of Object Oriented Programming Lec.4

DYNAMIC POLYMORPHISM:

Dynamic Polymorphism:Suppose a sub class overrides a particular

method of the super class. Let’s say, in the program we create an object of the subclass and assign it to the super class reference. Now, if we call the overridden method on the super class reference then the sub class version of the method will be called.

As the method to call is determined at runtime, this is called dynamic binding or late binding.