Presentation is loading. Please wait.

Presentation is loading. Please wait.

Extending Classes Through Inheritance

Similar presentations


Presentation on theme: "Extending Classes Through Inheritance"— Presentation transcript:

1 Extending Classes Through Inheritance
B.Ramamurthy 7/3/2019 B.Ramamurthy

2 Inheritance Inheritance is the act of deriving a new class from an existing one. A primary purpose of inheritance is to reuse existing software. Original class used to derive a new class is called “super” class and the derived class is called “sub” class. Inheritance represents “is a” relationship between the superclass and the subclass. 7/3/2019 B.Ramamurthy 4

3 Syntax class subclass extends superclass { class definition } Example:
class Windstar extends FordCar // meaning it inherits from class Fordcar{ ....} Windstar MyCar; In this example, FordCar is the super-class and Windstar is a sub-class and MyCar is an object reference to Windstar class. MyCar = new Windstar(green); // object instantiated 7/3/2019 B.Ramamurthy

4 Representing the Relationship
BankClass has a has a has a Account [ ] MortgageSVC BrokerageSVC is a is a Savings Checking is a : use inheritance has a : use aggregation, or membership 7/3/2019 B.Ramamurthy 6

5 Example: Animal Hierarchy
is a Animal type Dog constructor name breed show In the code, note the call to super-class constructor “super” constructor (name) constructor (name , breed) 7/3/2019 B.Ramamurthy

6 Example : Words Book class super class Main class uses is a
Dictionary class super class subclass uses is a 7/3/2019 B.Ramamurthy

7 Modifers Modifiers are special words that are added in front of methods, data fields, classes to specify their nature. Visibility modifiers: private, public, protected Protected modifier is used when the entity needs to be available to the subclass but not to the public. 7/3/2019 B.Ramamurthy

8 Need for “protected” modifier
Employee public: name private: wage_rate and hours uses super class Application uses Employee class, Paid_Employee class Paid_Employee uses public compute_wages(); sub class 7/3/2019 B.Ramamurthy

9 Need for “protected” modifier
Employee is a super class, Paid_Employee is a sub class derived from it. If we make wage_rate and hours public they are available to the whole world. If you make them private then they are not accessible to the sub class unless there are set and get functions. Protected access control provides a mechanism for hiding details from the world but making it available to sub classes. 7/3/2019 B.Ramamurthy

10 Attributes (Data) Modifiers
Data with no modifier is visible to sub-class within the same package but not to sub-class outside the package. Private data is available only within the class in which it is defined. Protected is available to the class and all its sub-classes. Public is available to all class. 7/3/2019 B.Ramamurthy

11 Polymorphism We already discussed polymorphism in lecture 2 with Food as example. (Given in next slide) Polymorphism is ability to assume several different forms and automatic resolution of one of the forms depending on the (run time) type of the object. 7/3/2019 B.Ramamurthy

12 Abstract Class (ABC) and methods
An abstract class is a class in which one or more methods are declared but not defined. Declaration of the methods can be omitted since it make no sense to implement them in the superclass. Am abstract method has “abstract” modifier in front of it. ABC cannot be instantiated. Sole purpose of ABC is to provide an appropriate super class from which classes may inherit. Classes from which objects can be instantiated are called concrete classes. 7/3/2019 B.Ramamurthy

13 Example Food Abstract super class Pizza Hamburger HotDog subclasses
7/3/2019 B.Ramamurthy

14 Example for Superclass Framework
public abstract class EPA { \\ different data public abstract void emission_control();} class CA_EPA extends EPA { void emission_control () { //implementation } class NY_EPA extends EPA { void emission_control () { // implementation } 7/3/2019 B.Ramamurthy

15 Interface An interface essentially takes the abstract class concept to the extreme. When a class contains all abstract methods and/or constants, it can be implemented using an interface. It contains just constants, abstract methods or both. Many different implementations can be realized from a single interface. The concept of interface-implementation is the core of the JDK1.1 event model. 7/3/2019 B.Ramamurthy

16 Animal interface and implementations
dog spaniels show(){…} sound(){….} Special details added on to the general dog characteristics interface others animal Other implementations show(); sound(); 7/3/2019 B.Ramamurthy

17 Example GUI and mouse listerners EVENT LISTERNERS UPDATE GUI GUI
EVENT HANDLERS 7/3/2019 B.Ramamurthy

18 Summary Inheritance implements a very useful relationship between classes. Inheritance lets you extend and reuse existing classes / library of classes. Interface provides a framework for a system of classes. Interfaces are implemented by other classes. A class may implement more than one interface. 7/3/2019 B.Ramamurthy


Download ppt "Extending Classes Through Inheritance"

Similar presentations


Ads by Google