Unformatted text preview:

Inheritance part 3 Method overriding COMP 401 Spring 2013 Lecture 10 2 12 2013 Subclassing So Far A subclass inherits implementaDon details from its superclass Fields Direct access to public and protected elds No direct access to private elds Methods Access to public and protected methods No access to private methods Subclass constructors Should call superclass constructor with super as rst line Or chain to a di erent constructor Or rely on implicit call to super constructor with no parameters Subclass Method Polymorphism Subclass can overload methods in superclass Remember overloading is providing a di erent version of an exisDng method An example of polymorphism Method signature is di erent in some way lec10 v1 Overriding Methods A subclass can override a super class method by providing its own de niDon Method signature must be the same Original method is visible from subclass i e public protected or package level access lec10 v2 Override direcDve So what s with the funny Override line that Eclipse includes when generaDng a stub Known as a compiler direcDve Completely opDonal but useful Indicates that the method is intended to override a superclass method Compiler will complain if it does not detect a visible superclass method with the same method signature Helpful when you misspell a method name or a empt to override a method not visible to the subclass lec10 v3 Class Polymorphism Previously introduced the idea of is a relaDonships Between a class and interfaces implemented Between a class and its superclass hierarchy This is also an example of polymorphism Covariance TreaDng an instance of a subclass as a reference typed as the parent class This can be typed checked at compile type Contravariance TreaDng a reference typed as the parent class as an instance of a subclass Contravariance can not be type checked in advance at compile Dme Fails if the object is actually invariant with respect to the subclass lec10 v4 lec10 v4main Also demonstrates protected base class constructor A Covariant Conundrum Problem What should happen when an overriden method is called on a covariant reference C c obj new C B b obj B c obj A a obj A c obj System out println c obj m System out println b obj m System out println a obj m class A public int m return 0 class B extends A public int m return 1 class C extends B public int m return 2 What should these lines print SoluDon 1 Non virtual methods Let type of reference dictate which method de niDon is used C c obj new C B b obj B c obj A a obj A c obj System out println c obj m System out println b obj m System out println a obj m class A public int m return 0 class B extends A public int m return 1 class C extends B public int m return 2 These lines print 2 1 0 SoluDon 2 Virtual methods Use method de ned by actual type of object C c obj new C B b obj B c obj A a obj A c obj System out println c obj m System out println b obj m System out println a obj m class A public int m return 0 class B extends A public int m return 1 class C extends B public int m return 2 These lines print 2 2 2 Virtual Methods Di erent OOP languages choose to solve this problem in di erent ways C C Default is non virtual soluDon Programmer can force virtual soluDon by marking a method with a special virtual keyword Java Methods are always virtual No special keyword needed lec10 v5 A virtual problem Drawback to the always virtual approach Consider the situaDon in which a subclass just needs a method to do just a li le more In other words wants to execute a method as de ned in the superclass and then tweak the result Or maybe do something in advance of execuDng a method as de ned in the superclass Because methods are always virtual casDng this reference to superclass in order to get to method as de ned by the superclass won t work lec10 v6 It s a bird it s a plane it s the super keyword The super keyword provides exactly this ability to invoke methods on an instance as it is understood at the superclass lec10 v7 Whence inheritance Related classes with common internals Note not just common behavior SpecializaDon aker the fact of exisDng classes Assignment 3 Assignment 3 is posted


View Full Document

UNC-Chapel Hill COMP 401 - comp401sp13lecture10Inheritancepart3

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Loading Unlocking...
Login

Join to view comp401sp13lecture10Inheritancepart3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view comp401sp13lecture10Inheritancepart3 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?