You are on page 1of 23

UNIT III INHERITANCE

UNIT III : INHERITANCE Inheritance is one of the corner stones of Object oriented programming. Because it allows hierarchical classifications. In terminology of java, a class that is inherited is called a super class. The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of super class. It inherits all of the instance variables and methods defined by the super class and adds its own, unique elements. Hierarchical abstractions powerful way to manage abstraction is through the use of hierarchical classification. This allows to brea! the comple" systems into manageable pieces. for e"ample, a car is a single object for outside but the car consists of several subsystems such as steering, brea!s, sound system, seat belts etc. gain the sound system consists of radio, #$# player etc. %ere we manage the comple"ity of a car system through the use of hierarchical classification. %ere everything we view in this world is an object. The topmost hierarchy for every object is material object. Base class& suppose in the above e"ample, car is the base class. and the sub classes are steering, brea!s, engine etc The behaviour of the child class is always an e"tension of the properties of the associated with the parent class. Subclass: 'onsider the relationship associated with the parent class and subclass Instances of a sub class must possess all the data areas associated with the parent class. Instances of a sub(class must implement through inheritance at least some or all functionalities defined for the parent class. Thus an instance of a child class can mime the behaviour of the parent class and should be indistinguishable from the instance of parent class if substituted in similar situation. This creates a problem because we use so many forms of inheritance. To solve this, we use principle of substitutability . Substitutability : #ef& )The principle of substitutability* says that if we have two classes + , and +B, such that class B is sub(class of class, it should be possible to substitute instances of class B for instance of class in any situation with no observable effect.

3.1

Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

Subtype: The term subtype often refers to a subclass relationship in which the principle of substitutability is maintained. statically typed languages -'../ place much more emphasis on principle of substitutability than dynamically typed languages -0mall Tal!/ The reason for this is the statically typed languages tend to characterize objects by their class and dynamically typed languages tend to characterize by behaviour. That is the subtype us determined in statically typed languages by their class and dynamically typed languages by their behaviour. Forms of inheritance: Inheritance is used in variety of ways. The following list represents general abstract categories and is not intended to be e"haustive. 1. 0ub(classing for Specialization& The most common use of inheritance and sub(classing is for specialization. In this the derived class is specialized form of the parent class and satisfies the specifications of the parent in all relevant aspects. This is the most ideal form of inheritance, something that a good design should strive for eg& class window provides general windowing operations such as moving, resizing etc. specialized sub class te"t edit window inherits the window opens. and in addition provides the windows to display te"tual material eg& class 2arent 3 int i4156 void show-/ 3 0ystem.out.println-)i value4*.i/6 7 7 class 'hild e"tends 2arent 3 88 show-/ will be called by 'hild Object in main class 7 3.2 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

9. 0ub(classing for Specification& In the classes maintained a certain common interface i.e, they implement the same methods the parent class can be the combination of implemented operations and the operations are differed to child class. The child merely implements behaviour describe but not implemented in the parent. In such classes the parent class is mostly !nown as abstract specification class. :g& abstract class 2arent 3 int i6 abstract void show-/6 88 only specifications 7 class 'hild e"tends 2arent 3 i4956 void show-/ 3 0ystem.out.println-)i 4*.i/6 7 7 ;. 0ub(classing for Construction& class can often inherit , almost all of its desired functionality from the parent class perhaps changing only the names of the methods used to interface to the class are modifying the arguments in a certain fashion. This may be true if the new class and the parent class failed to share the relationship. eg& class 2arent 3 int i4156 void show-/ 3 0ystem.out.println-)i value4*.i/6 3.3 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

7 7 class 'hild e"tends 2arent 3 i4;56 void display-/ 88 own implementation of child class 3 0ystem.out.println-)i value4*.i/6 7 7 <. 0ub(classing for e tension. In sub(classing for generalization modifies or e"tends on the e"isting functionality of an object. But sub(classing for e"tension acts totally new abilities. In the sub(classing for generalization it must override atleast one method from the parent and here we must atleast e"tend one method of the parent. :g& class 2arent 3 int i4156 void show-/ 3 0ystem.out.println-)i value4*.i/6 7 7 class 'hild e"tends 2arent 3 88 show-/ will be called by 'hild Object in main class void display-/ 88 new method is also implemented 3 3.4 0ystem.out.println-)This is child class*/6 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

7 7

=. 0ub(classing for limitation& 0ubclassing for limitation occurs when the behaviour of the sub(class is smaller or more restrictive than the behaviour of the parent class. :g& class 2arent 3 int i4156 void show-/ 3 0ystem.out.println-)i value4*.i/6 7 void display-/ 88 new method is also implemented 3 0ystem.out.println-)This is 2arent class*/6 7 7 class 'hild e"tends 2arent 3 88 show-/ will be called by 'hild Object in main class 88 display-/ may not use .i.e, all methods of 2arent will not be used 7 >. subclassing for Combination& common situation is that a sub class may represent the combination of features from two or more parent classes. 3.5 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

?or e"ample a class teaching assistant can be desired from both the parent classes teacher and assistant

!enefits of Inheritance:
various benefits of inheritance are 1. 08w @eusability& Aany programmers spend much of their time in rewriting code they have written many times before. 0o with inheritance code once written can be reused. 9. code sharing 'ode sharing occurs at two levels. t first level many users or projects can use the same class. In the second level sharing occurs when two or more classes developed by a single programmer as part of a project which is being inherited from a single parent class. %ere also code is written once and reused. This is possible through inheritance. ;. 'onsistency of inheritance& Bhen two or more classes inherit from the same superclass we are assured that the behaviour they inherit will be the same in all cases. Thus we can guarantee that interfaces to similar objects are in fact similar. <. software components& Inheritance provides programmers the ability to construct reusable s8w components. The goal behind these is to provide applications that require little or no actual coding. lready such libraries and pac!ages are commercially available. =. @apid 2rototyping& Bhen a s8w system is constructed largely out of reusable components development can be concentrated on understanding the new and unusual portion of the system. Thus s8w system can be generated more quic!ly and easily leading to a style of programming !nown as +@apid 2rototyping, or +e"ploratory programming, >. 2olymorphism and ?ramewor!& Cenerally s8w is written from the bottom up , although it may be designed from top(down. This is li!e building a wall where every 3.6 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

bric! must be laid on top of another bric! i.e., the lower level routines are written and on top of these slightly higher abstraction are produced and at last more abstract elements are generated. polymorphism permits the programmer to generate high level reusable components. D. Information %iding& 2rogrammer who reuses a software need only to understand the nature of the component and its interface. There is no need to have detailed information of the component and it is information hiding.

Costs of Inheritance:
lthough they are benefits with object oriented programming we must also consider the cost of inheritance. 1. Execution Speed& The inherited methods which must deal with orbitary sub( classes are often slower than specialized code. %ere efficiency is often misplaed. It is far better to develop a wor!ing system and monitor it. 9. Program Size: The use of any s8w library imposes a size penalty not imposed by systems constructed for a specific project. lthough the e"pense may be substantial and size of program becomes less important. ;. Message passing overhead: Aessage passing is costly by nature a more costly operation than simple procedure invocation <. Program Complexity: lthough object oriented programming is often touched as a solution to s8w comple"ity. the comple"ity increases when we use inheritance. But it is not considerable. Inheritances in "a#a: 1. 0ingle Inheritance 9. Aulti(level Inheritance ;. %ierarchical Inheritance 1. 0ingle Inheritance& In a class hierarchy when a child has one and only one parent and parent has one only child, that inheritance is said to be single inheritance.

3.7

Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

eg&

class 0tudent 3 int rollno6 void getEo-int no/ 3 rollno4no6 7 void putEo-/ 3 0ystem.out.println-)rollno4 *.rollno/6 7 3. Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

7 class Aar!s e"tends 0tudent 3 float mar!s6 void getAar!s-float m/ 3 mar!s4m6 7 void putAar!s-/ 3 0ystem.out.println-)mar!s4 *.mar!s/6 7 7 class #isplay 3 public static void main-0tring arFG/ 3 Aar!s ob4new Aar!s-/6 ob.getEo-<</6 ob.putEo-/6 ob.getAar!s->>/6 ob.putAar!s-/6 7 7 9. Aulti(level Inheritance In a class hierarchy, when a class is derived from already derived class then that inheritance is said to be multi(level inheritance. 3.! Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

eg&

class 0tudent 3 3.1" Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

int rollno6 void getEo-int no/ 3 rollno4no6 7 void putEo-/ 3 0ystem.out.println-)rollno4 *.rollno/6 7 7 class Aar!s e"tends 0tudent 3 float mar!s6 void getAar!s-float m/ 3 mar!s4m6 7 void putAar!s-/ 3 0ystem.out.println-)mar!s4 *.mar!s/6 7 7 class 0ports e"tends Aar!s 3 float score6 void get0core-float scr/ 3 3.11 score4scr6 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

7 void put0core-/ 3 0ystem.out.println-)score4 *.score/6 7 7 class #isplay 3 public static void main-0tring arFG/ 3 0ports ob4new 0ports-/6 ob.getEo-<</6 ob.putEo-/6 ob.getAar!s-==/6 ob.putAar!s-/6 ob.get0core-H=/6 ob.put0core-/6 7 7 ;. %ierarchical inheritance& In a class hierarchy, when a parent class has two or more than two child classes then, the inheritance is said to be hierarchical inheritance.

3.12

Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

e.g&

class 0tudent 3 int rollno6 void getEo-int no/ 3 rollno4no6 7 void putEo-/ 3 3.13 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

0ystem.out.println-)rollno4 *.rollno/6 7 7 class Aar!s e"tends 0tudent 3 float mar!s6 void getAar!s-float m/ 3 mar!s4m6 7 void putAar!s-/ 3 0ystem.out.println-)mar!s4 *.mar!s/6 7 7 class 0ports e"tends 0tudent 3 float score6 void get0core-float scr/ 3 score4scr6 7 void put0core-/ 3 0ystem.out.println-)score4 *.score/6 7 7 class #isplay 3 3.14 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

public static void main-0tring arFG/ 3 Aar!s s14new Aar!s-/6 0ports s94new 0ports-/6 s1.getEo-<</6 s1.putEo-/6 s1.getAar!s-<=/6 s1.putAar!s-/6 s9.getEo-<</6 s9.putEo-/6 s9.get0core-I=/6 s9.put0core-/6 7 7 $olymorphism: It is a mechanism of having multiple forms. In other words polymorphism is a mechanism of defining an interface to represent a general class actions. %etho& '#erri&in(: In a class hierarchy when a method of a subclass has the same name and type signature has that of a method in its superclass, then the method in the subclass is said to be override the method in the superclass. Bhen this is the case the mechanism called )method overriding* and such methods are called overridden methods. Bhen overridden method is using base class object the base class version is e"ecuted and if it is called by subclass, the subclass version is e"ecuted. eg& class 3 3.15 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

void callAe-/ 3 0ystem.out.println-)I am version of superclass */6 7 7 class B e"tends 3 void callAe-/ 3 0ystem.out.println-)I am version of subclass B*/6 7 7

class #isplay 3 public static void main-0tring arFG/ 3 B ob4new B-/6 ob.callAe-/6 88 B,s callAe-/ will be e"ecuted 88 i.e, callAe-/ 7 7 bstract Jeyword& 1. To declare abstract methods 9. To declare abstract classes 1. 3.16 bstract Aethod& Prepared by PHANI KISHORE ROMPICHRLA ,s callAe-/ overridden by B,s

UNIT III INHERITANCE

It is a method in which we have only method declaration but it will not have definition or body. bstract methods aare declared with abstract !eyword and terminated by semicolon -)/ 0ynta"& abstract return_type MethodName(parameter_list ; In a class hierarchy when a superclass containing an abstract method. all the subclasses of that superclass must override the base class method. If the child fails to override the superclass abstract method, the child class must also be abstract. %ence abstract method follows the process of method overriding. 9. bstract 'lasses& class which consists atleast one abstract method is called abstract class. The class definition must be preceded by abstract !eyword. 0ynta"&

abstract class className ! """"" """"" abstract returntype MethodName(parameter_list ! """"""" """"""" # """"""" """""""
$$ it can include concrete methods also

#
eg& abstract class 0hape 3.17 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

3 abstract void callAe-/6 7 class sub e"tends 0hape 3 void callAe-/ 3 0ystem.out.println-)I am sub,s Aethod*/ 7 7 class #emo bstract 3 public static void main-0tring arFG/ 3 sub s4new sub-/6 s.callAe-/6 7 7

final !eyword& final !eyword is used for the following purposes 1. To define final datamembers 9. To define final methods ;. To define final classes 1. final datamembers& 3.1 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

when a data member is declared as final, its value can,t be changed through out the program. %ence final variables can be treated as +java constants, %ence when a datamember is declared as final its value must be assigned immediately. eg& final double pie4;.1<6 9. final methods& when a method is declared as final its value cant be overridden at all synta"& final returntype AethodEame-2arameterKist/ 3 ((((((( ((((((( 7 Bhen a base class method is declared as final no subclass of that superclass will be allowed to override the base class final method eg& final void show-/ 3 (((((( (((((( 7 ;. final class& when a class is declared as a final it cant be inherited at all. %ence we cant have subclasses for final classes.

super !eyword&
It is used for referring the immediate superclass of the sub class super !eyword is used to access super class data members from its subclass. eg& class 3 int "6 7 3.1! Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

class B 3 int "6 B-int p, int q/ 3 super."4p6 "4q6 7 void display-/ 3 0ystem.out.println-)super variable4*.super."/6 0ystem.out.println-)sub class variable4*. "/6 7 7 class #emo0uper 3 public static void main-0tring arFG/ 3 B ob4new B-15,95/6 ob.display-/6 7 7 class 88 superclass variable is initialized 88 subclass variable is initialized

super uses:
1/super !eyword is used to access superclass methods from its subclass eg& above e"ample 3.2" Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

9/ super !eyword is used to access superclass constructor from its subclass eg& class 3 int ",y6 -int m, int n/ 3 "4m6 y4n6 7 7 class B 3 int z6 B-int p, int q, int r/ 3 super-p,q/6 z4r6 7 void display-/ 3 0ystem.out.println-)super *.super.y/6 class variables4*.super.".* 88 superclass variable is initialized 88 subclass variable is initialized

0ystem.out.println-)sub class variable4*. z/6 7 7 class #emo0uper 3 public static void main-0tring arFG/ 3.21 Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

3 B ob4new B-15,95,;5/6 ob.display-/6 7 7

%ember access rules s java is an object(oriented programming language, data is given more importance regarding its access. #ata is to be protected against unauthorized access. In order to protect data in a well built way, data is organized into three categories. a/ 2ublic data b/ 2rivate data c/ 2rotected data public, private L protected are called access specifiers. The following table illustrates the member access rules The members declared as public can be accessed any where, any class and any pac!age. The members declared as private can be accessed only by the methods of same class where the private members are declared. The members declared as protected cannot be accessed in non( subclasses of different pac!ages in rest of the cases they can be accessed. If you want any member to be accessed in the subclasses of other pac!ages they can be declared as protected. These members are being protected from the access facility of non(subclasses of other pac!ages. The members declared by nothing, li!e of no modifier is being placed before a member of a class then, these members can be access only up to pac!age. This occurs by default. #ifferent pac!ages are not allowed to access these no modifier members.
$ublic Same class Same $ac*a(e 3.22 Mes Mes $rotecte& Mes Mes No %o&ifier Mes Mes $ri#ate Mes Eo

Prepared by PHANI KISHORE ROMPICHRLA

UNIT III INHERITANCE

Same class Same $ac*a(e No+sub Classes ,ifferent $ac*a(e Sub Classes ,ifferent $ac*a(e Non+Subclasses Mes Eo Eo Eo Mes Mes Eo Eo Mes Mes Mes Eo

3.23

Prepared by PHANI KISHORE ROMPICHRLA

You might also like