Presentation is loading. Please wait.

Presentation is loading. Please wait.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract.

Similar presentations


Presentation on theme: "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract."— Presentation transcript:

1 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract Classes

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Defining Classes with Inheritance  To explain the concept of inheritance, we will consider an example of a class roster.  The class roster should contain both undergraduate and graduate students.  Each student’s record will contain his or her name, three test scores, and the final course grade.  The formula for determining the course grade is different for graduate students than for undergraduate students.

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Defining Classes with Inheritance  There are two broad ways to design the classes to model undergraduate and graduate students. Define a Student class which includes a variable to specify which type of student. We can define two unrelated classes, one for undergraduates and one for graduates. We can model the two kinds of students by using classes that are related in an inheritance hierarchy.  Two classes are unrelated if they are not connected in an inheritance relationship.

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Defining Classes with Inheritance  If two object types are expected to share common behaviors and data, it is better to design their classes using inheritance.  Using unrelated classes in such an instance will result in duplicating code common to both classes. It also makes it more difficult to create collections that include both types

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Student Classes  For this example, we will design three classes: Student UndergraduateStudent GraduateStudent  The Student class will incorporate behavior and data common to both UndergraduateStudent and GraduateStudent objects.  The UndergraduateStudent class and the GraduateStudent class will each contain behaviors and data specific to their respective objects.

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Student Classes  The Student class is the superclass.  The UndergraduateStudent and GraduateStudent classes are descendants of the Student class. They are subclasses of Student A subclass extends its superclass.  Sibling classes are those that share the common ancestor class.

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. UML Diagram  A superclass Student and its subclasses GraduateStudent and UndergraduateStudent.

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Polymorphism  Polymorphism allows a single variable to refer to objects from different classes. For example, if we declare Student student; We can say student = new Student(); // or student = new GraduateStudent(); // or student = new UndergraduateStudent(); * However, a variable of class X may not refer to an object from the superclass or sibling classes of X.

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Using Polymorphism  We can maintain our class roster using an array, combining objects from the Student, UndergraduateStudent, and GraduateStudent classes. Student roster [40]; roster[0] = new GraduateStudent(); roster[1] = new UndergraduateStudent(); roster[2] = new UndergraduateStudent(); and so on. * Or use an ArrayList ArrayList roster = new ArrayList (); Roster.add(new GraduateStudent()); Roster.add(new UnderGraduateStudent());

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object Diagram  The roster array with elements referring to instances of GraduateStudent or UndergraduateStudent classes.

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Using Polymorphism  To compute the course grade using the roster array, we execute for (int i=0; i<numberOfStudents; i++){ roster[i].computeCourseGrade(); } If roster[i] refers to a GraduateStudent, then the computeCourseGrade method of the GraduateStudent class is executed. If roster[i] refers to an UndergraduateStudent, then the computeCourseGrade method of the UndergraduateStudent class is executed.

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Polymorphism  The computeCourseGrade method is called polymorphic because the message refers to methods from different classes depending on the object referenced by roster[i].  Polymorphism, as you can see, permits the smooth and easy extension and modification of a program.

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Polymorphism  The instanceof operator can help us learn the class of an object. Student x = new UndergraduateStudent(); if(x instanceof UndergraduateStudent ){ System.out.println( “X is an undergraduate”); }else { System.out.println( “X is a graduate student”); } * Note: If your classes are well-designed, you should not need to do this very often.

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Access Modifiers  We have already seen two access modifiers for instance variables and methods. public data members and methods are accessible to everyone. private data members and methods are accessible only to instances of the class.  There is a third access modifier that is used with inheritance The modifier protected makes a data member or method visible and accessible to the instances of the class and the descendant classes.

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Member Accessibility class Super{ public int public_Super_Field; protected int protected_Super_Field; private int private_Super_Field; public Super(){ public_Super_Field = 10; protected_Super_Field = 20; private_Super_Field = 30; }... }

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Member Accessibility class Sub extends Super { public int public_Sub_Field; protected int protected_Sub_Field; private int private_Sub_Field; public Sub(){ public_Sub_Field = 100; protected_Sub_Field = 200; private_Sub_Field = 300; }... }

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. UML Diagram  A graphical representation of superclasses and subclasses with public, private, and protected members.

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Member Accessibility  A public member is accessible to any method.  A private member is accessible only to the methods that belong to the same class. Use accessor and mutator functions just as you would from any external class.

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Public Member Accessibility class Client { public void test(){ Super mySuper = new Super(); Sub mySub = new Sub(); int i = mySuper.public_Super_Field; int j = mySub.public_Super_Field; // inherited by mySub int k = mySub.public_Sub_Field; }  The above three statements are valid.

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Private Member Accessibility class Client { public void test(){ Super mySuper = new Super(); Sub mySub = new Sub(); int l = mySuper.private_Super_Field; int m = mySub.private_Sub_Field; int n = mySub.private_Super_Field; }  The above three statements are invalid.

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Protected Member Accessibility  A protected member is accessible only to the methods that belong to the same class or to the descendant classes.  It is inaccessible to the methods of an unrelated class.

22 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Protected Members class Client { public void test(){ Super mySuper = new Super(); Sub mySub = new Sub(); int o = mySuper.protected_Super_Field; int p = mySub.protected_Sub_Field; int q = mySub.protected_Super_Field; }  The statements above are invalid.

23 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Outside Access  The difference between public, private, and protected modifiers. Only public members are visible from outside.

24 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Subclass Access  Everything except the private members of the Super class is visible from a method of the Sub class.

25 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Member Accessibility  If a member X, whether inherited or defined in a class, is accessible from an instance of the class, then X is also accessible from all instances of the same class.  Data members accessible from an instance are also accessible from other instances of the same class.

26 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance and Constructors  Unlike members of a superclass, constructors of a superclass are not inherited by its subclasses.  You must define a constructor for a class or use the default constructor added by the compiler.

27 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance and Constructors  A class definition such as Class Person { public void sayHello() { System.out.println(“Well, hello.”); } is equivalent to Class Person { public Person(){ super(); } public void sayHello() { System.out.println(“Well, hello.”); }

28 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance and Constructors  The statement super(); calls the superclass’s constructor.  If the class declaration does not explicitly designate the superclass with the extends clause, then the class’s superclass is the Object class.

29 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance and Constructors  If you declare a constructor, then no default constructor is added to the class. If you define a class as class MyClass { public MyClass(int x){... } then the statement MyClass test = new MyClass(); is invalid because MyClass has no matching constructor.

30 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance and Constructors  If the constructor you define does not contain an explicit call to a superclass constructor, then the compiler adds the statement super(); as the first statement of the constructor. This will fail if the superclass does not have a default constructor.

31 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance and Constructors  If a class has a superclass that is not the Object class, then a constructor of the class should make an explicit call to a constructor of the superclass.  Always provide a constructor for every class you define. Don’t rely on default constructors.

32 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Superclasses and Methods  When we define a superclass, we do not always need to create any instances of the superclass. Sometimes you don't have enough information to write methods at the superclass level  Depending on whether we need to create instances of the superclass, we must define the class differently.  We will study examples based on the Student superclass defined earlier.

33 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Student Classes : Approach 1  Student Must Be Undergraduate or Graduate If a student must be either an undergraduate or a graduate student, we only need instances of UndergraduateStudent or GraduateStudent. We can enforce this by defining the Student class so that no instances may be created of it.

34 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Classes  An abstract class is a class defined with the modifier abstract. No instances can be created from an abstract class.  Abstract classes and interfaces have some properties in common.

35 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Methods  An abstract method is a method whose header includes the keyword abstract, and it ends with a semicolon instead of a method body.  A class is abstract if the class contains an abstract method or does not provide an implementation of an inherited abstract method.  The methods in an interface are abstract automatically.

36 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Classes and Abstract Methods  We say a method is implemented if it has a method body.  If a subclass has no abstract methods and no unimplemented inherited abstract methods, then the subclass is no longer abstract, and instances may be created of it.  An abstract class must contain the keyword abstract in its definition.

37 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. UML Diagrams  In a class diagram, we represent an abstract class by using the keyword abstract.

38 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Student Classes : Approach 2  Student Does Not Have to Be Undergraduate or Graduate.  In this case, we may design the Student class in one of two ways. We can make the Student class instantiable. We can leave the Student class abstract and add a third subclass, OtherStudent, to handle a student who does not fall into the UndergraduateStudent or GraduateStudent categories.

39 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Student Classes  With the first approach, we delete the keyword abstract from the class definition and provide a method body for computeCourseGrade. class Student {... public void computeCourseGrade(){ int total = 0; for (int i=0; i<NUM_OF_TESTS; i++){ total += test[i]; } if (total/NUM_OF_TESTS >=50){ courseGrade = “Pass”; }else{ courseGrade = “No Pass”; } }... }  This design allows us to create an instance of Student to represent a nonregular student.

40 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Student Classes  In the second approach, we leave the Student class abstract. We define a third subclass, OtherStudent: class OtherStudent extends Student { public void computeCourseGrade(){ int total = 0; for (int i=0; i<NUM_OF_TESTS; i++){ total += test[i]; } if (total/NUM_OF_TESTS >=50){ courseGrade = “Pass”; }else{ courseGrade = “No Pass”; }

41 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. UML Diagram  An object diagram of the abstract superclass Student and its three subclasses.

42 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Decisions  The best approach depends on the particular situation.  When considering design options, we can ask ourselves which approach allows easier modification and extension.  Finally, private methods and static methods may not be declared abstract.

43 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance versus Interface  Java interface and inheritance both model an IS-A relationship: class ButtonHandler implements ActionListener Class SavingsAccount extends Account  We say “ButtonHandler is an ActionListener” and “SavingsAccount is an Account.”  However, their uses are very different. The Java interface is used to share common behavior (only method headers) among the instances of different classes. Inheritance is used to share common code (including both data members and methods) among the instances of related classes.

44 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance versus Interface  In your program designs, remember Use the Java interface to share common behavior. Use inheritance to share common code.  If an entity A is a specialized form of another entity B, then model them by using inheritance. Declare A as a subclass of B.

45 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. In-Class Exercise  Imagine that you are writing classes to be used in a program for registering vehicles at the DMV  Sketch an inheritance hierarchy for the following classes. vehicle car truck motorcycle trailer commercial vehicle non-commercial vehicle  Feel free to add additional classes


Download ppt "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract."

Similar presentations


Ads by Google