1 / 25

Class Inheritance Part II: Overriding and Polymorphism

Class Inheritance Part II: Overriding and Polymorphism. Corresponds with Chapter 10. The Object Class. Object is the root class of all classes in Java All other classes are descendents of Object Object is part of the java.lang package Useful Object methods:

hhagler
Télécharger la présentation

Class Inheritance Part II: Overriding and Polymorphism

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 II:Overriding and Polymorphism Corresponds with Chapter 10

  2. The Object Class • Object is the root class of all classes in Java • All other classes are descendents of Object • Object is part of the java.lang package • Useful Object methods: • equals -- tests for equality of value of two different objects • clone – instantiates a copy of an object (only if it implements the Cloneable inteface). • getClass – returns the class to which an object belongs • toString – returns a string of that object (by default, its class name and id, but this can be overridden).

  3. What is Polymorphism? • Polymorphism = the ability of object oriented programs to generate different behavior with identical method calls. • Dynamic Binding = run-time decision on which version of the method will execute (based on the instance’s class type) • Note that reference variables can point to instances of the reference’s class OR to instances of the reference’s subclasses. • When calling an instance method through a reference variable, the version of the method that actually executes is NOT based on the type (class) of the reference variable. It is based on the type (class) of the instance that the reference variable points to.

  4. Implementing Polymorphism in Java • Override methods in subclasses • Declare reference variables of the superclass type • Instantiate superclass-type reference variables with superclass instances OR • Instantiate superclass-type reference variables with subclass instances • Perform method calls of overridden methods via the superclass variables • If the superclass-type reference variable is pointing to a superclass instance, it is the superclass version of the overridden method that will be called BUT • If the superclass-type reference variable is pointing to a subclass instance, it is the subclass version of the overridden method that will be called • Therefore, it is the instance’s class (NOT the reference’s class) that determines which version of the method is called. • Therefore, it may not be known at compile time which version is called…it is not until instantiation (at run-time) that this is known: • This is dynamic binding.

  5. Even though there is not extends keyword, Circle is a subclass of Object. Object is the root of ALL classes in Java Overriding the Object class’s toString method

  6. In this example we are NOT overriding the Circle class’s findArea method Overriding the Circle class’s toString method

  7. If no explicit super call is made in the subclass constructor, the superclass’s default constructor is invoked by default. Overriding the Circle class’s findArea method Overriding the Circle class’s toString method

  8. UML Diagram of Classes used in this Example Object is the root of all classes We are overriding the Object class’s toString in all the descendent classes All attributes and operations are inherited by subclasses Super class no findArea at Cylinder level, therefore use the inheritedfindArea of Circle findArea at Sphere level overrides findArea of Circle subclasses Each subclass has its own version of findVolume, which is not at the superclass level.

  9. An example of using polymorphism (application class)

  10. An example of using polymorphism (application class)

  11. An example of using polymorphism (application class) • Note: the array is of type Circle (the superclass) •  thus the elements can point to ANY kind of Circle object (including instances of subclasses). But the instances that are created may be of any descendent class of Circle. Here, we see that, depending on the user’s choice, each element of the Circle array may be pointing at a Circle object, a Cylinder object, or a Sphere object. This will not be known until the program executes and the user makes a choice.

  12. An example of using polymorphism (application class) Yet, a call to the method is being done via a Circle reference. How do we know which version of the method to call? This reference could point to instances of different types! Answer – we know this because of dynamic binding…the version that is called will be determined by the instance that is pointed at.

  13. An example of using polymorphism (application class) Dynamic binding is possible because the Java Virtual Machine knows the specific class of all objects. You can see the class of a particular instance by using the getClass() method of the Object class.

  14. An example of using polymorphism (application class) If an object is referred to in a String context, its toString method is invoked Since the three classes have their own toString methods, the version invoked depends on the instance that is referenced…polymorphism and dynamic binding in action again!

  15. An example of using polymorphism (application class) The instanceof keyword is another useful way of determining the class of an object. It returns a boolean value indicating whether an object is an instance of a specified class.

  16. An example of using polymorphism (application class) If attempting to call a subclass method (not declared at the superclass) via a superclass reference, you will get a syntax error. Thus, you will need to cast the reference as a subclass reference before making the call. In this case, findVolume is declared for Cylinder and Sphere, but not for CircleWithAccessors, so casting is necessary. Note that the instanceof operator enabled me to see whether a call to findVolume was appropriate.

  17. What is Type Casting? • Treating a variable as if it is a different data type than what it was declared as • Why? Because sometimes you need to perform operations on the object that are not available for the declared variable type • Let’s consider the circles array and the necessity to call findVolume…..

  18. Why we need to cast… • The circles array is declared: • Circle [] circles • In order to call findVolume() on an instance pointed at by an element of the circles array, we would do this: • circles[i].findVolume() • But, there is no findVolume method of the Circle class! If we did the above line, it would result in a syntax error. As far as the compiler is concerned, we are attempting to call a method on a Circle instance. Solution: use type casting

  19. Cast comes first because of parentheses Dot operator is then applied to a Cylinder reference instead of a Circle reference How to Cast • Precede the variable identifier with the name of the data type you want to cast it as in parentheses: • (Cylinder)circles[i] • The above line treats circles[i] as a reference to a Cylinder object instead of as a reference to a CircleWithAccessors object. The text (Cylinder) is called a cast operator. • Note: operator precedence rules dictate that the dot operator takes place before the cast operator…therefore, in order to cast before attempting the member access, you need to use parentheses to override normal precedence: • ((Cylinder)circles[i]).findVolume();

  20. circles i 0 1 2 Let’s see what happens when we create an array of superclass references that point to subclass instances main’s frame args Frame Stack Heap

  21. Sphere object Circle object radius radius Cylinder object 5.0 5.0 5.0 2.0 args length radius circles i 0 1 2 Assume the first time user selects circle, 2nd time user select cylinder, and 3rd time user selects Sphere…. main’s frame Therefore, the array has Circle pointers, but each object could be any descendent of Circle. Frame Stack Heap

  22. Circle findArea frame Sphere object Circle object this radius radius Cylinder object 5.0 5.0 2.0 5.0 args radius length 0 circles i 0 1 2 Dynamic binding at work! The first instance is a Circle object. main’s frame Frame Stack Heap

  23. Circle findArea frame Sphere object Circle object this radius radius Cylinder object 5.0 5.0 2.0 5.0 args radius length 1 circles i 0 1 2 Dynamic binding at work! The 2nd instance is a Cylinder object, which does not have its own findArea method, so the superclass’s version is invoked. main’s frame Frame Stack Heap

  24. Sphere findArea frame Sphere object Circle object this radius radius Cylinder object 5.0 5.0 2.0 5.0 args radius length 0 circles i 0 1 2 Dynamic binding at work! The 3rd instance is a Sphere object, which has its own findArea method, so the overriding method is called. main’s frame Frame Stack Heap

  25. Other Concepts to Consider • Abstract class • A class that contains some non-implemented (abstract) methods… • Methods that contain a signature but no method body • Abstract classes cannot be instantiated, but can be superclasses of instantiable subclasses. • Abstract classes can have variables • Declared using the abstract keyword • Interface • A “class” that contains ONLY non-implemented methods • Declared using the interface keyword. • Inheritance done via the implements keyword instead of extends. • Interfaces cannot be instantiated, but can be “implemented” by “subclasses” • Interfaces cannot have variables, only constants • Inner class • A class that is declared nested inside another class declaration • Its scope is only within the class in which it is declared • Can refer to any members of its outer class.

More Related