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

eg& class 2arent 3 int i4156 void show-/ 3 0ystem. In such classes the parent class is mostly !nown as abstract specification class.i/6 7 7 .println-)i 4*. 0ub(classing for Construction& class can often inherit .println-)i value4*. 0ub(classing for Specification& In the classes maintained a certain common interface i.UNIT III INHERITANCE 9. :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. they implement the same methods the parent class can be the combination of implemented operations and the operations are differed to child class.3 Prepared by PHANI KISHORE ROMPICHRLA . This may be true if the new class and the parent class failed to share the relationship.out.i/6 3. The child merely implements behaviour describe but not implemented in the parent. 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..out.e.

But sub(classing for e"tension acts totally new abilities.4 0ystem.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.println-)This is child class*/6 Prepared by PHANI KISHORE ROMPICHRLA .out. :g& class 2arent 3 int i4156 void show-/ 3 0ystem. 0ub(classing for e tension.println-)i value4*.out.println-)i value4*.56 void display-/ 88 own implementation of child class 3 0ystem.UNIT III INHERITANCE 7 7 class 'hild e"tends 2arent 3 i4. In sub(classing for generalization modifies or e"tends on the e"isting functionality of an object.out.i/6 7 7 <. 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.

subclassing for Combination& common situation is that a sub class may represent the combination of features from two or more parent classes.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 .e.i.out. all methods of 2arent will not be used 7 >.out. :g& class 2arent 3 int i4156 void show-/ 3 0ystem. 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.i/6 7 void display-/ 88 new method is also implemented 3 0ystem.5 Prepared by PHANI KISHORE ROMPICHRLA . 3.println-)i value4*.UNIT III INHERITANCE 7 7 =.

t first level many users or projects can use the same class. %ere also code is written once and reused. 08w @eusability& Aany programmers spend much of their time in rewriting code they have written many times before. lready such libraries and pac!ages are commercially available. 9. code sharing 'ode sharing occurs at two levels. or +e"ploratory programming. although it may be designed from top(down. The goal behind these is to provide applications that require little or no actual coding. software components& Inheritance provides programmers the ability to construct reusable s8w components. Thus we can guarantee that interfaces to similar objects are in fact similar. This is li!e building a wall where every 3. 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. .6 Prepared by PHANI KISHORE ROMPICHRLA . 0o with inheritance code once written can be reused. Thus s8w system can be generated more quic!ly and easily leading to a style of programming !nown as +@apid 2rototyping. 2olymorphism and ?ramewor!& Cenerally s8w is written from the bottom up . =. '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.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. @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.. >. This is possible through inheritance. <.

lthough the e"pense may be substantial and size of program becomes less important. Program Size: The use of any s8w library imposes a size penalty not imposed by systems constructed for a specific project. the comple"ity increases when we use inheritance. Aulti(level Inheritance . the lower level routines are written and on top of these slightly higher abstraction are produced and at last more abstract elements are generated.. %ere efficiency is often misplaed. Inheritances in "a#a: 1. Message passing overhead: Aessage passing is costly by nature a more costly operation than simple procedure invocation <. .e. 0ingle Inheritance 9. 1. Costs of Inheritance: lthough they are benefits with object oriented programming we must also consider the cost of inheritance.7 Prepared by PHANI KISHORE ROMPICHRLA . D. But it is not considerable... 0ingle Inheritance& In a class hierarchy when a child has one and only one parent and parent has one only child. polymorphism permits the programmer to generate high level reusable components. Program Complexity: lthough object oriented programming is often touched as a solution to s8w comple"ity. 9. Execution Speed& The inherited methods which must deal with orbitary sub( classes are often slower than specialized code. 3.UNIT III INHERITANCE bric! must be laid on top of another bric! i. There is no need to have detailed information of the component and it is information hiding. %ierarchical Inheritance 1. Information %iding& 2rogrammer who reuses a software need only to understand the nature of the component and its interface. It is far better to develop a wor!ing system and monitor it. that inheritance is said to be single inheritance.

out. 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.println-)rollno4 *.rollno/6 7 3.

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.putEo-/6 ob. 3.! Prepared by PHANI KISHORE ROMPICHRLA .out. when a class is derived from already derived class then that inheritance is said to be multi(level inheritance. Aulti(level Inheritance In a class hierarchy.getEo-<</6 ob.putAar!s-/6 7 7 9.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.getAar!s->>/6 ob.

1" Prepared by PHANI KISHORE ROMPICHRLA .UNIT III INHERITANCE eg& class 0tudent 3 3.

println-)mar!s4 *.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.mar!s/6 7 7 class 0ports e"tends Aar!s 3 float score6 void get0core-float scr/ 3 3.out.out.11 score4scr6 Prepared by PHANI KISHORE ROMPICHRLA .UNIT III INHERITANCE int rollno6 void getEo-int no/ 3 rollno4no6 7 void putEo-/ 3 0ystem.println-)rollno4 *.

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

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 e.

14 Prepared by PHANI KISHORE ROMPICHRLA .score/6 7 7 class #isplay 3 3.println-)mar!s4 *.out.mar!s/6 7 7 class 0ports e"tends 0tudent 3 float score6 void get0core-float scr/ 3 score4scr6 7 void put0core-/ 3 0ystem.println-)score4 *.println-)rollno4 *.out.UNIT III INHERITANCE 0ystem.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.

getEo-<</6 s9. then the method in the subclass is said to be override the method in the superclass.15 Prepared by PHANI KISHORE ROMPICHRLA .getAar!s-<=/6 s1. Bhen overridden method is using base class object the base class version is e"ecuted and if it is called by subclass.putAar!s-/6 s9.put0core-/6 7 7 $olymorphism: It is a mechanism of having multiple forms.get0core-I=/6 s9.getEo-<</6 s1.putEo-/6 s9.putEo-/6 s1. 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. the subclass version is e"ecuted. Bhen this is the case the mechanism called )method overriding* and such methods are called overridden methods.UNIT III INHERITANCE public static void main-0tring arFG/ 3 Aar!s s14new Aar!s-/6 0ports s94new 0ports-/6 s1. eg& class 3 3.

s callAe-/ will be e"ecuted 88 i.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.s .callAe-/6 88 B.16 bstract Aethod& Prepared by PHANI KISHORE ROMPICHRLA .out. To declare abstract methods 9. To declare abstract classes 1.s callAe-/ overridden by B.out. callAe-/ 7 7 bstract Jeyword& 1. 3.println-)I am version of superclass */6 7 7 class B e"tends 3 void callAe-/ 3 0ystem.e.UNIT III INHERITANCE void callAe-/ 3 0ystem.

 If the child fails to override the superclass abstract method. all the subclasses of that superclass must override the base class method. bstract methods aare declared with abstract !eyword and terminated by semicolon -)/ 0ynta"& abstract return_type MethodName(parameter_list .  %ence abstract method follows the process of method overriding. 9. The class definition must be preceded by abstract !eyword. the child class must also be abstract.17 Prepared by PHANI KISHORE ROMPICHRLA .UNIT III INHERITANCE It is a method in which we have only method declaration but it will not have definition or body.  In a class hierarchy when a superclass containing an abstract method. 0ynta"& abstract class className ! """"" """"" abstract returntype MethodName(parameter_list ! """"""" """"""" # """"""" """"""" $$ it can include concrete methods also # eg& abstract class 0hape 3. bstract 'lasses& class which consists atleast one abstract method is called abstract class.

1 Prepared by PHANI KISHORE ROMPICHRLA .. final datamembers& 3. To define final classes 1. To define final datamembers 9.println-)I am sub.UNIT III INHERITANCE 3 abstract void callAe-/6 7 class sub e"tends 0hape 3 void callAe-/ 3 0ystem. To define final methods .callAe-/6 7 7 final !eyword& final !eyword is used for the following purposes 1.s Aethod*/ 7 7 class #emo bstract 3 public static void main-0tring arFG/ 3 sub s4new sub-/6 s.out.

UNIT III INHERITANCE when a data member is declared as final. %ence final variables can be treated as +java constants.1! Prepared by PHANI KISHORE ROMPICHRLA . 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. final class& when a class is declared as a final it cant be inherited at all..1<6 9. %ence when a datamember is declared as final its value must be assigned immediately. eg& class 3 int "6 7 3. %ence we cant have subclasses for final classes.t be changed through out the program. eg& final double pie4.. 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 . its value can.

2" Prepared by PHANI KISHORE ROMPICHRLA .out.UNIT III INHERITANCE class B 3 int "6 B-int p.println-)sub class variable4*.println-)super variable4*."/6 0ystem.95/6 ob."4p6 "4q6 7 void display-/ 3 0ystem.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.super. int q/ 3 super.out. "/6 7 7 class #emo0uper 3 public static void main-0tring arFG/ 3 B ob4new B-15.

int q.q/6 z4r6 7 void display-/ 3 0ystem.println-)super *. int r/ 3 super-p.super.21 Prepared by PHANI KISHORE ROMPICHRLA .UNIT III INHERITANCE 9/ super !eyword is used to access superclass constructor from its subclass eg& class 3 int ".y/6 class variables4*.out.out.println-)sub class variable4*.".super. z/6 7 7 class #emo0uper 3 public static void main-0tring arFG/ 3.* 88 superclass variable is initialized 88 subclass variable is initialized 0ystem.y6 -int m. int n/ 3 "4m6 y4n6 7 7 class B 3 int z6 B-int p.

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

UNIT III INHERITANCE Same class Same $ac*a(e No+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 .ifferent $ac*a(e Sub Classes .