240 likes | 418 Views
Inheritance. B.Ramamurthy. Object-Oriented Principles. OOP. Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems. Polymorphism -- Many forms of same function -- Runtime Binding -- Abstract Classes -- Interfaces
E N D
Inheritance B.Ramamurthy B.Ramamurthy
Object-Oriented Principles OOP Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems Polymorphism -- Many forms of same function -- Runtime Binding -- Abstract Classes -- Interfaces -- Uniformity Encapsulation (class) -- Information Hiding -- Separation of Interface and Implementation -- Standardization -- Access Control mechanisms (private /public) B.Ramamurthy
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 Encapsulation B.Ramamurthy
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 B.Ramamurthy
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. B.Ramamurthy
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. B.Ramamurthy
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 B.Ramamurthy
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) B.Ramamurthy
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 B.Ramamurthy
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.} Class Structure for Bank Problem classes of Bank hierarchy B.Ramamurthy
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. B.Ramamurthy
Example: Employee Hierarchy 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 B.Ramamurthy
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. Need for “protected” modifier : limited access B.Ramamurthy
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. Attributes (Data) Modifiers B.Ramamurthy
Example: class Automobile 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 } Ramamurthy
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 B.Ramamurthy
Completely abstract class or interface Concrete super class abstract class At least one method abstract. Use extends to make sub class. Use extends to make sub class A sub class implements the interface Inheritance Continuum B.Ramamurthy
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. B.Ramamurthy
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. B.Ramamurthy
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 } Example for Superclass Framework B.Ramamurthy
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. B.Ramamurthy
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. B.Ramamurthy
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 Inheritance through Interfaces B.Ramamurthy
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. B.Ramamurthy