Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.

Similar presentations


Presentation on theme: "1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to."— Presentation transcript:

1 1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to take an existing class called the base class or superclass and derive from it a new class called the derived class or subclass that inherits all the properties (data and methods) of the base class. Inheritance facilitates what is called the IS-A relationship because the derived class object IS-A instance of the base class object. The HAS-A relationship is implemented using a technique called composition rather than inheritance.

2 2 Basic Java Syntax public class Derived extends Base { // Any members that are not listed are inherited // unchanged except for constructors // public members // Constructor(s) if the default is not acceptable // Base methods that are to change in Derived // Additional public methods // private members // Additional data fields (generally private) // Additional private methods }

3 3 Accessing base class data If we want the derived class to have access to the data members of the base class they should be declared protected rather than private. Declaring data members as protected or public violates the spirit of encapsulation and information hiding Overriding base class methods If a method is implemented in the base class and is not overridden in the derived class then the base class method can be called from the derived class like any other method belonging to the class

4 4 Abstract classes Sometimes we want to create related objects by deriving them from an abstract base class. This enables us to treat them all the same but to have them behave polymorphically. That is we can invoke an operation through a base class reference and the operation appropriate to the actual type will be automatically selected.

5 Interface / Abstract Class Interface – Used to describe “what” capabilities you want a class to have – Has no (non final) data fields – All methods in an interface are public abstract – Comparable Interface Abstract Class – Can have some fields and concrete methods – Can have some public abstract methods

6 Payroll Program For Employees We have two types of Employees – SalariedEmp (data: id, name, annualSalary) – HourlyEmp (data: id, name, payRate, hours) – Both have methods computePay( ) and raise(amt) Designer Decides to Create superclass Employee – Should Employee be an interface, concrete class, or abstract class? – Data field: id, name – Methods: constructor, getter methods, toString Define the constructor – what does it do?

7 UML

8 Superclass Subclass - Questions For classes Employee and HourlyEmp – What is the relationship between the classes (superclass, subclass) and what is inherited? – Relationship between superclass and subclass constructor – What is a no-parameter (default) constructor


Download ppt "1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to."

Similar presentations


Ads by Google