1 / 21

Features of Object Oriented Programming Lec.4

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 .

eve
Télécharger la présentation

Features of Object Oriented Programming Lec.4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Features of Object Oriented Programming • Lec.4

  2. 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.

  3. 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.

  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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. public class Object { // constructor public Object(); // public instance methods Public boolean equals(Object obj); public String toString(); // protected instance methods protected void finalize() . . . }

  11. 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; • }

  12. // instance methods • public String getName() • { • Return employeeName; • } • public String getDepartment() • { • Return employeeDept; • } • Public intgetLengthOfService() • { • Return lengthOfService; • } • public float getHolidays() • { • Return holidayEntitlement; • } • }

  13. 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.

  14. 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.

  15. public class Technician extends Employee • { • // instance variable • protected float holidays; • // constructor • public Technician(String name, String department, intyearsService) • { • super(name, department, yearsService); • } • }

  16. 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.

  17. 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.

  18. 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.

More Related