130 likes | 145 Vues
Chapter 10 Thinking in Objects. Principle of OOP. Inheritance Abstraction Encapsulation Polymorphism Also know as (APIE). Inheritance and Composition. A class can extend another class. Inheritance implements the “ IS a” relationship between objects
E N D
Principle of OOP • Inheritance • Abstraction • Encapsulation • Polymorphism • Also know as (APIE)
Inheritance and Composition • A class can extend another class. • Inheritance implements the “ IS a” relationship between objects • A class can implements interface, methods members from its parent class.
Inheritance and Composition • Is- A and Has –A • Inheritance VS Composition • Favor Composition • Polymorphism • Interfaces
Is-A and Has-A relationship • Classes depend on another class • Classes are related via Is –A or Has –A relationship • Cat-> Animal = IS-A • Car->Wheel, Engine =Has -A • Train->Wheel,Engine=Has -A • Car,Truck-> Vehicle= Is-A • HAS-A=Composition (compose) IS-A=Inheritance(subtype) • Reuse code
Inheritance (Is-A) • Animal is a base class cat class inheritances from animal Animal Cat
Inheritance Syntax Java Supports only public inheritance A is a Superclass and B is a Subclass
Inheritance Syntax In order to call the constructor of superclass(A) the keyword super is used
Composition (Has-A) • Something is made up of other parts • Car contains wheel and engine • It is also called aggregation car wheel Engine
Composition (Has-A):aggregation Composition(HAS-A) simply mean use of instance variables that are references to other objects. For example: Maruti has Engine, or House has Bathroom
Inheritance Lab • Follow the instructions provided on Lab9