Presentation is loading. Please wait.

Presentation is loading. Please wait.

More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and.

Similar presentations


Presentation on theme: "More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and."— Presentation transcript:

1 More about inheritance Exploring polymorphism

2 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and dynamic type overriding dynamic method lookup protected access

3 02/12/2004Lecture 8: More about inheritance3 The inheritance hierarchy

4 02/12/2004Lecture 8: More about inheritance4 Conflicting output CD: A Swingin' Affair (64 mins)* Frank Sinatra tracks: 16 my favourite Sinatra album video: The Matrix (136 mins) Andy & Larry Wachowski must see if interested in virtual reality! title: A Swingin' Affair (64 mins)* my favourite Sinatra album title: The Matrix (136 mins) must see if interested in virtual reality! What we want What we now have

5 02/12/2004Lecture 8: More about inheritance5 The problem The print method in Item only prints the common fields. Inheritance is a one-way street: –A subclass inherits the superclass fields. –The superclass knows nothing about its subclass’s fields.

6 02/12/2004Lecture 8: More about inheritance6 Attempting to solve the problem Place print where it has access to the information it needs. Each subclass has its own version. But Item ’s fields are private. Database cannot find a print method in Item.

7 02/12/2004Lecture 8: More about inheritance7 Static type and dynamic type A more complex type hierarchy requires further concepts to describe it. Some new terminology: –static type –dynamic type –method dispatch/lookup

8 02/12/2004Lecture 8: More about inheritance8 Static and dynamic type Car c1 = new Car(); What is the type of c1? Vehicle v1 = new Car(); What is the type of v1?

9 02/12/2004Lecture 8: More about inheritance9 Static and dynamic type The declared type of a variable is its static type. The type of the object a variable refers to is its dynamic type. The compiler’s job is to check for static- type violations. Item item = (Item) iter.next(); item.print(); // Compile-time error.

10 02/12/2004Lecture 8: More about inheritance10 Overriding: the solution print method in both super- and subclasses. Satisfies both static and dynamic type checking.

11 02/12/2004Lecture 8: More about inheritance11 Overriding Superclass and subclass define methods with the same signature. Each has access to the fields of its class. Superclass satisfies static type check. Subclass method is called at runtime – it overrides the superclass version. What becomes of the superclass version?

12 02/12/2004Lecture 8: More about inheritance12 Method lookup No inheritance or polymorphism. The obvious method is selected.

13 02/12/2004Lecture 8: More about inheritance13 Method lookup Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match.

14 02/12/2004Lecture 8: More about inheritance14 Method lookup Polymorphism and overriding. The ‘first’ version found is used.

15 02/12/2004Lecture 8: More about inheritance15 Method lookup summary The variable is accessed. The object stored in the variable is found. The class of the object is found. The class is searched for a method match. If no match is found, the superclass is searched. This is repeated until a match is found, or the class hierarchy is exhausted. Overriding methods take precedence.

16 02/12/2004Lecture 8: More about inheritance16 Super call in methods Overridden methods are hidden...... but we often still want to be able to call them. An overridden method can be called from the method that overrides it. –super.method(...) –Compare with the use of super in constructors.

17 02/12/2004Lecture 8: More about inheritance17 Calling an overridden method public class CD {... public void print() { super.print(); System.out.println(" " + artist); System.out.println(" tracks: " + numberOfTracks); }... }

18 02/12/2004Lecture 8: More about inheritance18 Method polymorphism We have been discussing polymorphic method dispatch. A polymorphic variable can store objects of varying types. Method calls are polymorphic. –The actual method called depends on the dynamic object type.

19 02/12/2004Lecture 8: More about inheritance19 The Object class’s methods Methods in Object are inherited by all classes. Any of these may be overridden. The toString method is commonly overridden: –public String toString() –Returns a string representation of the object.

20 02/12/2004Lecture 8: More about inheritance20 Overriding toString public class Item {... public String toString() { String line1 = title + " (" + playingTime + " mins)"); if(gotIt) { return line1 + "*\n" + " " + comment + "\n"); } else { return line1 + "\n" + " " + comment + "\n"); }... }

21 02/12/2004Lecture 8: More about inheritance21 Overriding toString Explicit print methods can often be omitted from a class: –System.out.println(item.toString()); Calls to println with just an object automatically result in toString being called: –System.out.println(item);

22 02/12/2004Lecture 8: More about inheritance22 Protected access Private access in the superclass may be too restrictive for a subclass. The closer inheritance relationship is supported by protected access. Protected access is more restricted than public access. We still recommend keeping fields private. –Define protected accessors and mutators.

23 02/12/2004Lecture 8: More about inheritance23 Access levels

24 02/12/2004Lecture 8: More about inheritance24 Review The declared type of a variable is its static type. –Compilers check static types. The type of an object is its dynamic type. –Dynamic types are used at runtime. Methods may be overridden in a subclass. Method lookup starts with the dynamic type. Protected access supports inheritance.

25 02/12/2004Lecture 8: More about inheritance25 Concepts method polymorphism overriding dynamic method lookup static types dynamic types super protected

26 02/12/2004Lecture 8: More about inheritance26 Class Exercise Design a Local Area Network application. This application consists of the following classes: Node, Workstation, Fileserver and Printserver, and Packet. A Packet is an object transporting textual information to a given LAN-element. A Workstation is a special Node in the sense that it is the only one which can put Packets on the LAN.

27 02/12/2004Lecture 8: More about inheritance27 Class Exercise (II) A Fileserver saves the contents of the Packets to a file (which is created with the same name as the Fileserver with a.txt extension). The contents of the Packets should be put in file separated by '@'. A Printserver prints the contents of the Packet to standard output. The LAN we are simulating is circular such that Packets cannot drop off the LAN. This means that each LAN-element should keep a link to the next LAN-element.

28 02/12/2004Lecture 8: More about inheritance28 Class Exercise (III) Any LAN-element should be able to stop a Packet. Of course this can only be done if the Packet is addressed to it. Make sure that a Packet does not circle indefinitely. Design each of the classes: specify static and instance fields and methods. Describe the purpose of all of those. If required, specify the inheritance relationship between them. Were do you use polymorphism (if ever)?


Download ppt "More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and."

Similar presentations


Ads by Google