Extending Classes Through Inheritance B Ramamurthy 2232021 B
Extending Classes Through Inheritance B. Ramamurthy 2/23/2021 B. Ramamurthy 1
Inheritance z Inheritance is the act of deriving a new class from an existing one. z A primary purpose of inheritance is to reuse existing software. z Original class used to derive a new class is called “super” class and the derived class is called “sub” class. z Inheritance represents “is a” relationship between the superclass and the subclass. 2/23/2021 B. Ramamurthy 2
Syntax class subclass extends superclass { class definition } Example: class Windstar extends Ford. Car // meaning it inherits from class Fordcar{. . } Windstar My. Car; In this example, Ford. Car is the super-class and Windstar is a sub-class and My. Car is an object reference to Windstar class. My. Car = new Windstar(green); // object instantiated 2/23/2021 B. Ramamurthy 3
Representing the Relationship Bank. Class has a Account [ ] is a Checking has a Mortgage. SVC Brokerage. SVC Savings is a : use inheritance has a : use aggregation, or membership 2/23/2021 B. Ramamurthy 4
Example: Animal Hierarchy Animal is a type constructor show Dog name breed In the code, note the call to super-class constructor “super” 2/23/2021 B. Ramamurthy constructor (name) constructor (name , breed) 5
Example : Words Book class super class Main class uses is a Dictionary class subclass 2/23/2021 B. Ramamurthy 6
Modifers z. Modifiers are special words that are added in front of methods, data fields, classes to specify their nature. z. Visibility modifiers: private, public, protected z. Protected modifier is used when the entity needs to be available to the subclass but not to the public. 2/23/2021 B. Ramamurthy 7
Need for “protected” modifier Employee public: name private: wage_rate and hours uses super class Paid_Employee public compute_wages(); uses sub class 2/23/2021 B. Ramamurthy Application uses Employee class, Paid_Employee class 8
Need for “protected” modifier z Employee is a super class, Paid_Employee is a sub class derived from it. z If we make wage_rate and hours public they are available to the whole world. z If you make them private then they are not accessible to the sub class unless there are set and get functions. z Protected access control provides a mechanism for hiding details from the world but making it available to sub classes. 2/23/2021 B. Ramamurthy 9
Attributes (Data) Modifiers z Data with no modifier is visible to sub-class within the same package but not to sub-class outside the package. z Private data is available only within the class in which it is defined. z Protected is available to the class and all its subclasses. z Public is available to all class. 2/23/2021 B. Ramamurthy 10
Polymorphism z. We already discussed polymorphism in lecture 2 with Food as example. (Given in next slide) z. 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. 2/23/2021 B. Ramamurthy 11
Abstract Class (ABC) and methods z An abstract class is a class in which one or more methods are declared but not defined. z Declaration of the methods can be omitted since it make no sense to implement them in the superclass. z Am abstract method has “abstract” modifier in front of it. z ABC cannot be instantiated. z Sole purpose of ABC is to provide an appropriate super class from which classes may inherit. z Classes from which objects can be instantiated are called concrete classes. 2/23/2021 B. Ramamurthy 12
Example Food Pizza Hamburger Abstract super class Hot. Dog subclasses 2/23/2021 B. Ramamurthy 13
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 } 2/23/2021 B. Ramamurthy 14
Interface z An interface essentially takes the abstract class concept to the extreme. z When a class contains all abstract methods and/or constants, it can be implemented using an interface. z It contains just constants, abstract methods or both. z Many different implementations can be realized from a single interface. z The concept of interface-implementation is the core of the JDK 1. 1 event model. 2/23/2021 B. Ramamurthy 15
Animal interface and implementations implementation 1 dog interface animal show(); sound(); 2/23/2021 show(){…} sound(){…. } others Other implementations B. Ramamurthy spaniels Special details added on to the general dog characteristics 16
Example z. GUI and mouse listerners EVENT LISTERNERS GUI 2/23/2021 UPDATE GUI B. Ramamurthy EVENT HANDLERS 17
Summary z Inheritance implements a very useful relationship between classes. z Inheritance lets you extend and reuse existing classes / library of classes. z Interface provides a framework for a system of classes. z Interfaces are implemented by other classes. z A class may implement more than one interface. 2/23/2021 B. Ramamurthy 18
- Slides: 18