You are on page 1of 8
8.11 INHERITANCE: EXTENDING A CLASS Reusability is yet another aspect of OOP paradigm. It is always nice if we could reuse something that already exists rather than creating the same all over again. Java supports this concept. Java classes can be reused in several ways. This is basically done by creating new classes, reusing the properties of existing ones. The mechanism of deriving a new class from an old one is called inheritance. The old class is known as the base class or super class. or parent class and the new one is called the subclass or derived class or child class. The inheritance allows subclasses to inherit all the variables and methods of their parent classes. Inheritance may take different forms: 4, Single inheritance (only one super class) 2. Multiple inheritance (several super classes) 3. Hierarchical inheritance (one super class, many subclasses) A A | ¥ | y B B (Ec! D (2) Single inheritance _(b) Hierarchical inheritance A A B \I El (d) Multiple inheritance ° (c) Multilevel inheritance [GEER Fors of inheritance 4, Multilevel inheritance (Derived from a derived class) These forms of inheritance are shown in Fig. 8.3. Java does not directly implement multiple inheritance. However, this concept is implemented using a secondary inheritance path in the form of interfaces. Interfaces are discussed in Chapter 10. Defining a Subclass Asubclass is defined as follows: ( ) class subclassname extends superclassname variables declaration; methods declaration; The keyword extends signifies that the properties of the superclassname are extended to the subclassname. The subclass will now contain its own variables and methods as well as those of the superclass. This kind of situation occurs when we want to add some more properties to an existing class without actually modifying it. Program 8.6 illustrates the concept of single inheritance. Multilevel Inheritance common requirement in object-oriented programming is the use of a derived class as a super class. Java Grandfather A_| Superclass supports this concept and uses it extensively in building its class library. This concept allows us to build a chain of classes as shown in Fig. 8.4. The class A serves as a base class for the derived class B which in tum serves as a base class for the derived class C. The chain ABC is known as inheritance nig path. A derived class with multilevel base classes is declared as follows. class A { Father Intermediate superciass Subclass be Multilevel inheritance } class B extends A // Firstlevel } class C extends B // Second level { } This process may be extended to any number of levels. The class C can inherit the members of both A and B as shown in Fig. 8. Avo tye a (WUrxidle ao a program Bo 6H oF sirens ern po rk Jeowa.. io gs egy? 4 2 Clas Basie Pel. \ aed Ma oe Base. a Sh fe 4 Unt nix, Void Limpuk Cunt 2) 00 ! R 4 m= aH, ‘ Ly Gye. ve Prcemditnn CD Beale aaa +E f fio Hi Ye Wes 2) Bo .e a bret lendigind oe. wh ( rd WS) Sor ay alg. ig lon ES Be VOid heolerloke.c ) atl () al \ Geet We @meg Siicgy NRE CD Sheds: Rac ees ouck. Probl, (oi. Us An): \ e 4 Cg MOI Le Ao Chow Pa entends Rr ; urt hb, 4, gross. Void dealctlow ( O de en) / to, Syphon. oud. prcertr- CB. da va 4); Vo idl Duplo ( ) Gr se = nt Super rtd > 1424 Spsern.oud. printer (* a 7 ey 4 ctars Caron SER bei) BY 2 De we EM, public. Atoctie noid! main. (4 gee { of (Tents bout, bate don unt a 2 Trdequ. ae arts), ) Da ob > OM (ap, Obs: ver put oe Me ar ean) avg. h caleetate. @ je she 29 Aya uy 2 aC oma. dd coleliahect (5. Obs3 + Se | 4 29 (| (a Bog Pail} Mos | TakOy \ Abstract claws is Kowa am closkrack See nile Te needs ko be onrctomded and wekled ak ® Differmmer between ppb prbstrcoet Chou = Bape rnc MCE hove. cb stink cord mon ob Strack metal Q. Abstr oles does Tk Auppark pn. ple aT %. Ab ytrpck clout Com vowe. firchk mon firel, Moke, ee tobe - Lt ean pro wide UmmplomentoXio n of Verdenpock B. An AbRACK Choy | Com extend ayvo-te Os Heo ee 4- 2. 3. iG. * Eich heen 4 Dtefng. vd Cree _ Prdenpoce com have OT bip pobutrenet metal - Bh. Sanp porcty pra pla Utarhercitian ce cea hax a Bl deo ist nkenpoce Cam prtovicle qe torphorrentotion OP obhtnnck Low

You might also like