You are on page 1of 11

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.

com

Inheritance
Inheritance can be defined as the process where one object acquires the properties of another. I nheritance is used to reuse the code. For example the sedan car, hatch back, suv all these share similar properties and behaviors. I nstead of rewriting the properties and behavior in each and every class ( class Sedan, class hatchback, class Suv) we can just define the common properties and behavior in a class c alled Car which can be inherited by all other different types of Cars. In order inherit the properties and behavior of one class i n another we simply use the key word extends.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com Syntax:

The class which inherits the other class is called as sub class or child class and the other class is called as Super class or parent class.

In the above figure Bicycle class is inherited by MountainBike, RoadBike, TandemBike. This is called as hierarchical inheritance .

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com

In the above figure Vehicle is inherited by Car. This is single inheritance.

In the above figure car inherits Vehicle class and RacingCar class inherits Car class. This scenario is called as Multilevel inheritance . Inheritance is always called is I S-A relationship. For example a Car is a Vehi cle. A RoadBike is Bicycle, so does the son is father if cl ass son extends father class. Below ven diagram will help you to understand why inheritance is called as IS- A relationship.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com

In the above figure class son extends father and mother. This scenario is calle d as multiple inheritance which is not p ossible in J AVA but you can achieve multiple inheritance if define father and mother as interfaces. Check interfac e document to know about the interface. When you create an Object of Son you can access the members i n Father class through the Son Object.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com

Method Overriding Given the above s cenario The Son class extended Father class so he got a home and also a bad behavior fr om his father . But lets say son doesnt smokes but he only drinks. Now we have a problem h ere. This can be resolved by overriding the badBehavior(int money) method as given below.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com

In the above scenario Son only drinks. What if Son not only drinks but also smokes. Will you add one more s tatement?. I f so then whats the point of inheritance( reusing the Code). We can resolve this issue by calling the method badBehaviour in parent class by using the key word super.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com Same way you can also access super class data members through super keyword.

Please take some time to think about how method overriding is different from overloading? Is method overriding can be an example for runtime polymorphism?

Has- A relationship There are two ways of accessing the class properties and methods or reusing the code. 1. Using inheritance (I S - A relationship) 2. By creating Objects of a class (H AS - A relationship) If you are reading this then you are already perfect in classes and Objects. Let me explain you how H AS- A rel ationship works . A Car is a real world object for which you will define a class. Car contains ma ny items such as Door, Music Sys tem, and Engine etc. These I tems are also real world objects for which you can define classes. An Engine has properties such as height, weight, behavior such as start(), stop(). Once you define classes for these items you can reuse them by creating their object in Car. So Car has engine, Car has Musi c System is a valid statement.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 www.felight.com

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

You might also like