1 / 25

Java Inheritance: Superclasses and Subclasses

Learn about inheritance in Java, including how subclasses inherit members from superclasses, implementing inheritance in Java, accessing superclass members, and overriding methods.

galice
Download Presentation

Java Inheritance: Superclasses and Subclasses

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class InheritancePart I Corresponds with Liang Chapter 10

  2. What is Inheritance? • Superclasses and subclasses • The subclass inherits the members of the base class • All instance member variables for the superclass will be present in an instance of the subclass • All instance methods of the superclass can be called with respect to an instance of the subclass

  3. Implementing Inheritance in Java • The extends keyword • specifies the superclass for a class • If class1 extends class2, then • class1 will be a subclass of class2, so • class1 will inherit all the members of class2. • The this keyword • Refers to the current class • The super keyword • Refers to the superclass • Can use it to call the superclass constructor • Can use it to access a superclass method when the subclass overrides this method • Accessing superclass members • The statements in the subclass methods can access any non-private (i.e. public or protected) members of the superclass.

  4. Superclasses and Subclasses There are similar examples in chapter 10

  5. A Cylinder1 class as a subclass of Circle Extends specifies that Cylindar1’s superclass is Circle The superclass The subclass

  6. A Cylinder1 class as a subclass of Circle Here, the super keyword is used to invoke the superclass’s constructor. The subclass The superclass

  7. A Cylinder1 class as a subclass of Circle Here, the subclass method is calling the inherited method of the superclass. The superclass The subclass

  8. A Cylinder1 class as a subclass of Circle If a local variable or parameter has the same name as a member variable, use the this keyword to resolve the ambiguity. The superclass The subclass

  9. A Cylinder1 class as a subclass of Circle Invoke constructor An application using the Cylinder1 class

  10. A Cylinder1 class as a subclass of Circle Calling subclass methods An application using the Cylinder class

  11. A Cylinder1 class as a subclass of Circle Calling inherited superclass methods An application using the Cylinder class

  12. main’s frame radius length A Cylinder1 instance is created on the heap…Note that the instance has the Cylinder1 class’s length member variable AND it inherits the Circle class’s radius member variable. Cylinder1 object args myCylinder Frame Stack Heap

  13. 5.0 radius 2.0 length main’s frame Cylinder1 constructor frame this radius length Cylinder1’s constructor is invoked with respect to the newly created instance. Cylinder1 object args myCylinder Frame Stack Heap

  14. 5.0 radius 2.0 length main’s frame Cylinder1 constructor frame Circle constructor frame this this 5.0 radius length 5.0 newRadius Circle’s constructor is invoked with respect to the newly created instance, and executes. Cylinder1 object args myCylinder Frame Stack Heap

  15. 5.0 radius 2.0 length main’s frame Cylinder1 constructor frame this 5.0 2.0 length radius Cylinder1’s constructor executes after Circle’s constructor terminates. Cylinder1 object args myCylinder Frame Stack Heap

  16. main’s frame 5.0 2.0 radius length After constructors terminate, the results from new are assigned into myCylinder. Cylinder1 object args myCylinder Frame Stack Heap

  17. Cylinder1 findVolume frame Circle findArea frame main’s frame this this 5.0 2.0 radius length Later in the program…… Cylinder object args myCylinder Frame Stack Heap

  18. Advantages of Inheritance • No need to “reinvent the wheel” • Can make use of pre-existing data and functionality • In previous example: • radius member variable was inherited • code for initializing radius (in constructor) was inherited • code for calculating area was inherited • Can “specialize” the inherited data and functionality • In previous example: • length member variable was created for subclass • code for initializing length (in subclass constructor) was created • code for calculating volume was created (making use of inherited area calculation)

  19. Overriding Methods • A subclass can override the methods of its superclass • Overriding is not the same as overloading! • Overriding = same signature as superclass method, with different method body and declared at the subclass. • If you want specialized behavior at the subclass, override the superclass method. • If you don’t want specialized behavior at the subclass, simply use the inherited superclass method.

  20. A Cylinder class that overrides the Circleclass’s findArea method Here, the subclass has its own findArea method…this overrides the findArea method of the superclass.

  21. A Cylinder class that overrides the Circleclass’s findArea method Can still make use of the superclass’s version of the overridden method through use of the super keyword..

  22. Unified Modeling Language • Static class diagram: • Boxes for representing classes • Top part of box contains name • Middle part contains attributes • Lower part contains operations • Solid line with empty arrow describes inheritance.

  23. Inheritance relationship is shown via a line with an triangular arrow from the subclass to the superclass. Classes Attributes Operations UML Diagram for Cylinder Visio drawing of UML diagram

  24. Cylinder’s findArea frame Circle’s findArea frame main’s frame this this 5.0 2.0 radius length If the findArea method is called on an instance of Cylinder…… Cylinder object args myCylinder Frame Stack Heap

  25. In-Class Group Exercise • Create a subclass of Circle called Sphere. • This class should inherit everything from Circle, but should override the findArea method and should include its own findVolume method. • The only difference between a Sphere and a Circle is the calculations of its dimensions: • The area of a sphere is: 4 * PI * r2 • The volume of a sphere is: 4/3 * PI * r3 Draw a UML diagram, and the write the code for the class.

More Related