Superclasses and subclasses Is a?" Relationship oObject"is an object of another class o Rectangle"is a quadrilateral Class Rectangle inherits from class Quadrilateral O Form tree-like hierarchical structures hyperclass Subc lasses Student Graduatestudent UndergraduateStudent Shape Circle Triangle Rectangle L。an Carload HomeImprovementLoan M。 tracer。ar Employee Facul tyMember sta£ Member ACcount CheckingAccount SavingsAccount ig.9.1 Some simple inheritanc e examp les in whic h the subc lass" is a"superclass
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Superclasses and Subclasses ⚫“Is a” Relationship Object “is an” object of another class ⚫Rectangle “is a” quadrilateral • Class Rectangle inherits from class Quadrilateral Form tree-like hierarchical structures
Superclasses and Subclasses Superclass Circle Circle methods Circle data Inheritance Subclass Cylinder Circle methods Circle data Cylinder Methods Cylinder Data uperclass class UML Diagram Circle Cylinder radius length getRadius atRadius FgetLength find Area length Find Volume Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Superclasses and Subclasses Superclass Circle Circle Methods Circle Data Inheritance Cylinder Circle Methods Cylinder Methods Circle Data Cylinder Data Subclass Circle -radius +getRadius +setRadius +findArea Cylinder -length +getLength +setLength +findVolume Superclass Subclass UML Diagram
//Cvlinder iava: Class definition for describing cvlinder public class Cylinder extends Circle i private double length =1; S Subclass supertype /* Return length */ subtype Circle Cylinder public double getLengtho i radius return length length getRadius setRadius getLength finda length find volume /* Set length */ public void setlength( double length)t this length= length; /* Return the volume of this cylinder * public double find volumed i return findareao* length; Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 // Cylinder.java: Class definition for describing Cylinder public class Cylinder extends Circle { private double length = 1; /** Return length */ public double getLength() { return length; } /** Set length */ public void setLength(double length) { this.length = length; } /** Return the volume of this cylinder */ public double findVolume() { return findArea() * length; } } Circle -radius +getRadius +setRadius +findArea Cylinder -length +getLength +setLength +findVolume Superclass Subclass supertype subtype
Cylinder cylinder=new Cylinder System. out printIn("The length is"+ cylinder. getLengthO) System. out println(" The radius is"+ cylinder. getRadiuso) System. out println("The volume of the cylinder is"+ cylinder. find Volume) ystem out printIn("The area of the circle is"+ cylinder. find Areao) Pravate data fields and mothods will not The output is be inherited in a subclass The length is 1.0 The radius is 1.0 The volume of the cylinder is 3.14159 The area of the circle is 3.14159 Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Cylinder cylinder = new Cylinder(); System.out.println("The length is " + cylinder.getLength()); System.out.println("The radius is " + cylinder.getRadius()); System.out.println("The volume of the cylinder is " + cylinder.findVolume()); System.out.println("The area of the circle is " + cylinder.findArea()); The length is 1.0 The radius is 1.0 The volume of the cylinder is 3.14159 The area of the circle is 3.14159 The output is Pravate data fields and mothods will not be inherited in a subclass
An inheritance hierarchy for university Community Members. CommunityMember is a direct superclass of Employee tRainer Communi tyMember is Faculty Employee Strat Alumi Administrator Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 An inheritance hierarchy for university CommunityMembers. CommunityMember Employee Student Faculty Staff Administrator Teacher Alumni CommunityMember is a direct superclass of Employee CommunityMemberis an indirect superclass of Faculty