Download presentation
Presentation is loading. Please wait.
1
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy
2
Object-Oriented Principles
OOP Encapsulation (class) -- Information Hiding -- Separation of Interface and Implementation -- Standardization -- Access Control mechanisms (private /public) Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems Polymorphism -- Many forms of same function -- Runtime Binding -- Abstract Classes -- Interfaces -- Uniformity 11/7/2018 B.Ramamurthy
3
Encapsulation Realized using the “class” construct
Encapsulate the data and provide access methods, update methods etc. related to the encapsulated data. Presentation: use a class diagram 11/7/2018 B.Ramamurthy
4
Classes and Relationships
A solution to a problem may contain many related classes. Five basic types of relationships among classes are defined by UML: Association Aggregation Composition Generalization (inheritance) Refinement 11/7/2018 B.Ramamurthy
5
Inheritance Classes in a typical application domain often exhibit some common properties and/or operations. In this case, the classes are associated through an inheritance relationship. Classes participating in a inheritance relationship can be visualized as a hierarchy. 11/7/2018 B.Ramamurthy
6
Inheritance (contd.) Inheritance relationship also represents generalization/specialization. Most general class is at the top of the hierarchy and the other classes are derived from this general class and specialized by adding or modifying properties and capabilities. 11/7/2018 B.Ramamurthy
7
Example: A Bank Hierarchy
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 composition 11/7/2018 B.Ramamurthy 6
8
Inheritance Mechanisms
Two mechanisms in Java for realizing inheritance: 1. Single inheritance through “extend”ing existing classes (both concrete and abstract) 2. Multiple inheritance through “interface” and “implementation”(s) 11/7/2018 B.Ramamurthy
9
Inheritance through “extends”
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 11/7/2018 B.Ramamurthy
10
Class Structure for Bank Problem
classes of Bank hierarchy class Savings extends Account {...} class Checking extends Account { …} class Bank { Account acctList[] = new Account(100); // 100 accounts are instantiated MortgageSVC someName1 = new MortgageSVC(); BrokerageSVC someName2 = new BrokerageSVC(); etc.} 11/7/2018 B.Ramamurthy
11
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. 11/7/2018 B.Ramamurthy
12
Example: Employee Hierarchy
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 11/7/2018 B.Ramamurthy
13
Need for “protected” modifier : limited access
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. 11/7/2018 B.Ramamurthy
14
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. 11/7/2018 B.Ramamurthy
15
Example: class Automobile
public … startEngine{ } public … accelerate{ } public Shift_gear; // in automatic cars //this will be private protected gloveCompartment; // you have access not the burglar private odometer; protected engine; // driver doesn’t access to it // but a mechanic does } 11/7/2018 Ramamurthy
16
Types of Genralization
Which one to use? Inheritance Abstract class is a partially defined class. It cannot be instantiated. Example: Federal EPA may set the policies (what is to be done) but their implementation (how) is done at state level. Extending a concrete class Extending an abstract class Implementing an interface 11/7/2018 B.Ramamurthy
17
Inheritance Continuum
Concrete super class Completely abstract class or interface Use extends to make sub class A sub class implements the interface At least one method abstract. Use extends to make sub class. 11/7/2018 B.Ramamurthy
18
Shapes Hierarchy Shapes Hierarchy Shape : abstract class
ShapePointCircleCylinder Circle extends Point super class Cylinder extends Circle super class You add on to or redefine super class methods and data. 11/7/2018 B.Ramamurthy
19
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 makes no sense to implement them in the super class. 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. 11/7/2018 B.Ramamurthy
20
Example for Superclass Framework
public abstract class EPA { \\ different data public abstract void emission_control(); other methods may be defined } class CA_EPA extends EPA { void emission_control () { //implementation } class NY_EPA extends EPA { void emission_control () { // implementation } 11/7/2018 B.Ramamurthy
21
Interface and Implementation
An interface is where all the methods are abstract. A Java class can extend only one class but can implement many interfaces. This is how multiple inheritance is realized in Java. 11/7/2018 B.Ramamurthy
22
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 Java event model. 11/7/2018 B.Ramamurthy
23
Inheritance through Interfaces
Interface provides a framework for a system of classes. Interfaces are implemented by other classes. A class may implement more than one interface. Java Event model exemplifies interface + implementation technique. Ex: MyApplet extends applet implements MouseListerner, MouseMotionListener 11/7/2018 B.Ramamurthy
24
Summary Inheritance implements a very useful relationship between classes. Inheritance lets you extend and reuse existing classes / library of classes. We have covered the fundamentals of OO(Design/Analysis/Programming) Your task is to relate the concepts to your projects. class, object, abstract class, inheritance, interface and implementation are the essential components of OOD/A/P. 11/7/2018 B.Ramamurthy
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.