Inheritance

384 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
384
On SlideShare
0
From Embeds
0
Number of Embeds
130
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Inheritance

  1. 1. Chapter 8Inheritance
  2. 2. © 2004 Pearson Addison-Wesley. All rights reserved 8-2Inheritance• Inheritance is a fundamental object-orienteddesign technique used to create and organizereusable classes• Chapter 8 focuses on: deriving new classes from existing classes the protected modifier creating class hierarchies indirect visibility of inherited members designing for inheritance
  3. 3. © 2004 Pearson Addison-Wesley. All rights reserved 8-3Inheritance• Lets say that we have designed a Java class calledPerson, with some attributes and methods• But now we want to add some extra attributes, butnot all people share these attributes, e.g. Bond students have a student-id, e.g. 12345678 Bond academic staff have a position, e.g. Professor• If we add both attributes to the existing Personclass, then all Person objects will have both astudent-id and a staff position. Not good.• It would be nice to create new classes in Java whichinherit the attributes and methods from anotherclass, but which we can extend with our own stuff.
  4. 4. © 2004 Pearson Addison-Wesley. All rights reserved 8-4Inheritance• Inheritance allows a software developer to derive anew class from an existing one• The existing class is called the parent class, orsuperclass, or base class• The derived class is called the child class orsubclass• As the name implies, the child inheritscharacteristics of the parent• That is, the child class inherits the methods anddata defined by the parent class• The new class can have extra attributes, extramethods, and it can also modify the behaviour ofthe original methods.
  5. 5. © 2004 Pearson Addison-Wesley. All rights reserved 8-5Inheritance• Inheritance relationships are shown in a UMLclass diagram using a solid arrow with an unfilledtriangular arrowhead pointing to the parent classVehicleCar• Proper inheritance creates an is-a relationship,meaning the child is a more specific version of theparent• The Car class is more specific than the Vehicleclass. More attributes and methods can be defined
  6. 6. © 2004 Pearson Addison-Wesley. All rights reserved 8-6Inheritance• Why is inheritance useful?• A programmer can tailor a derived class as neededby adding new variables or methods, or bymodifying the inherited ones• Software reuse is a fundamental benefit ofinheritance• By using existing software components to createnew ones, we capitalize on all the effort that wentinto the design, implementation, and testing of theexisting software
  7. 7. © 2004 Pearson Addison-Wesley. All rights reserved 8-7Creating Subclasses• In Java, we use the reserved word extends to establish an inheritancerelationship• See Words.java (page 440)• See Book.java (page 441)• See Dictionary.java (page 442)class Car extends Vehicle{// class contents: newattributes/methods}
  8. 8. © 2004 Pearson Addison-Wesley. All rights reserved 8-8The protected Modifier• Visibility modifiers affect the way that classmembers can be used in a child class• Variables and methods declared with privatevisibility cannot be referenced by name in a childclass• They can be referenced in the child class if theyare declared with public visibility -- but publicvariables violate the principle of encapsulation• There is a third visibility modifier that helps ininheritance situations: protected
  9. 9. © 2004 Pearson Addison-Wesley. All rights reserved 8-9The protected Modifier• The protected modifier allows a child class toreference a variable or method directly in the childclass• It provides more encapsulation than publicvisibility, but is not as tightly encapsulated asprivate visibility• A protected variable is visible to any class in thesame package as the parent class• Protected variables and methods can be shownwith a # symbol preceding them in UML diagrams
  10. 10. © 2004 Pearson Addison-Wesley. All rights reserved 8-10Class Diagram for WordsBook# pages : int+ pageMessage() :voidDictionary- definitions : int+ definitionMessage() :voidWords+ main (args : String[]) :void
  11. 11. © 2004 Pearson Addison-Wesley. All rights reserved 8-11The super Reference• Constructors are not inherited, even though theyhave public visibility• Yet we often want to use the parents constructorto set up the "parents part" of the object• The super reference can be used to refer to theparent class, and often is used to invoke theparents constructor• See Words2.java (page 445)• See Book2.java (page 446)• See Dictionary2.java (page 447)
  12. 12. © 2004 Pearson Addison-Wesley. All rights reserved 8-12The super Reference• A child’s constructor is responsible for calling theparent’s constructor• The first line of a child’s constructor should usethe super reference to call the parent’sconstructor• The super reference can also be used to referenceother variables and methods defined in theparent’s class
  13. 13. © 2004 Pearson Addison-Wesley. All rights reserved 8-13Overriding Methods• A child class can override the definition of aninherited method in favor of its own• The new method must have the same signature asthe parents method, but can have a different body• The type of the object executing the methoddetermines which version of the method isinvoked• See Messages.java (page 450)• See Thought.java (page 451)• See Advice.java (page 452)
  14. 14. © 2004 Pearson Addison-Wesley. All rights reserved 8-14Class Hierarchies• A child class of one parent can be the parent ofanother child, forming a class hierarchyBusinessKMart MacysServiceBusinessKinkosRetailBusiness
  15. 15. © 2004 Pearson Addison-Wesley. All rights reserved 8-15Class Hierarchies• Two children of the same parent are called siblings• Common features should be put as high in thehierarchy as is reasonable• An inherited member is passed continually down theline• Therefore, a child class inherits from all its ancestorclasses• There is no single class hierarchy that is appropriate forall situations

×