 Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

Download  Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

Post on 19-Jan-2016

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<p>COMP163</p> <p>Topics Inheritance introduction is a relationship &amp; has a relationship Super classes and Sub classes Types of inheritance Single inheritance Hierarchical inheritance Multilevel inheritance Multiple inheritance1IntroductionInheritance A form of software reuse in which a new class is created by absorbing an existing classs members and adding them with new or modified capabilities. Can save time during program development by basing new classes on existing proven and debugged high-quality software. Increases the likelihood that a system will be implemented and maintained effectively.2Introduction When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. Existing class is the superclass New class is the subclass Each subclass can be a superclass of future subclasses. A subclass can add its own fields and methods. A subclass is more specific than its superclass and represents a more specialized group of objects. The subclass exhibits the behaviors of its superclass and can add behaviors that are specific to the subclass. 3IntroductionThe private members of the superclass are private to the superclass.The subclass can directly access the public members of the superclass.The subclass can include additional data and method members.All data members of the superclass are also data members of the subclass. Similarly, the methods of the superclass (unless overridden) are also the methods of the subclass. 4is a relationship &amp; has a relationship The is a relationship is expressed with inheritance and has a relationship is expressed with composition.For Example:</p> <p>is a --- House is a Building</p> <p>class Building {</p> <p> .......</p> <p>}</p> <p>class House extends Building {</p> <p> .........</p> <p>} For Example:</p> <p>has a -- House has a bathroom</p> <p>class House{ Bathroom room = new Bathroom() ; .... public void getTotMirrors() { room.getNoMirrors(); .... } }5is a relationship &amp; has a relationship Inheritance Vs Composition</p> <p>Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses extends key word. </p> <p>Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom.</p> <p>Composition simply means using instance variables that refer to other objects. The class House will have an instance variable, which refers to a Bathroom object.6Superclasses and Subclasses Classes can be derived from other classes. The derived class (the class that is derived from another class) is called a subclass. The class from which its derived is called the superclass. </p> <p>7Superclasses and Subclasses In fact, in Java, all classes must be derived from some class. Which leads to the question "Where does it all begin?" The top-most class, the class from which all other classes are derived, is the Object class defined in java.lang. Object is the root of a hierarchy of classes.</p> <p>8Superclasses and Subclasses Example</p> <p>9Superclasses and Subclasses</p> <p> Figure shows a sample university community class hierarchy. Also called an inheritance hierarchy. </p> <p> Each arrow in the hierarchy represents an is-a relationship. </p> <p> Follow the arrows upward in the class hierarchy An Employee is a CommunityMember </p> <p> a Teacher is a Faculty member. </p> <p> CommunityMember is the direct superclass of Employee, Student and Alumnus and is an indirect superclass of all the other classes in the diagram. </p> <p> Starting from the bottom, you can follow the arrows and apply the is-a relationship up to the topmost superclass. 10Type of InhertianceFour kinds of inheritance in JAVA. They are.</p> <p> Single Inheritance (Only one super class).</p> <p>Multilevel Inheritance (Derived from a derived class).</p> <p> Hierarichal Inheritance (one Super class, many subclasses).</p> <p> Multiple Inheritance (Several Super classes).11Type of InheritancePictorial Representation:PersonStudentSingle InheritancePersonStudentMultilevel InheritanceDay ScholarPersonEmployeeStudentHierarchical InheritanceEmployeeStudentEmployee - StudentMultiple Inhertiance12Single Inheritance When a subclass is derived simply from it's parent class then this mechanism is known as single inheritance. In case of single inheritance there is only a sub class and it's parent class. It is also called one level inheritance. package yuc.edu.sa;public class SingleInhertiance { int x; int y; public void set(int p, int q){ x=p; y=q; } void Show(){ System.out.println(x); }} For Examplepackage yuc.edu.sa;public class SingleInhertianceApp extends SingleInhertiance { public static void main(String[ ] args) { SingleInhertiance singleInhertiance = new SingleInhertiance(); singleInhertiance.set(10, 20); singleInhertiance.Show(); }}13Hierarchical Inheritance One Parent class has many sub classes. And this is the concept of Hierarchical Inheritance. For Example:</p> <p>14Hierarchical Inheritance</p> <p>Output:</p> <p>15Multilevel Inheritance Derived from a derived class. Such concept is called Multilevel inheritance. For Example:</p> <p>16Multilevel Inheritance</p> <p>Output:</p> <p>17Multiple Inheritance The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface.18?19</p>

Recommended

View more >