210 likes | 333 Views
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 .
E N D
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.
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.
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.
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.
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.
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.
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.
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.
public class Object { // constructor public Object(); // public instance methods Public boolean equals(Object obj); public String toString(); // protected instance methods protected void finalize() . . . }
An Example of Inheritance • public class Employee • { • // constant • protected static float holidayEntitlement= 20.0; • // instance variables • protected String employeeName; • protected String employeeDept; • protected intlengthOfService; • // constructor • public Employee(String name, String department, intyearsService) • { • employeeName= name; • employeeDept = department; • lengthOfService = yearsService; • }
// instance methods • public String getName() • { • Return employeeName; • } • public String getDepartment() • { • Return employeeDept; • } • Public intgetLengthOfService() • { • Return lengthOfService; • } • public float getHolidays() • { • Return holidayEntitlement; • } • }
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.
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.
public class Technician extends Employee • { • // instance variable • protected float holidays; • // constructor • public Technician(String name, String department, intyearsService) • { • super(name, department, yearsService); • } • }
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.
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.
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.