07Inheritance - ITI 1121. Introduction to Computing II *...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of January 23, 2011 Abstract • Inheritance (part II) – Polymorphism * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. Circle Let’s complete the implementation of the class Circle . Where would you implement the method area() ? In the class Shape or int the class Circle ? Circle public class Circle extends Shape { private double radius; public double getRadius() { return radius; } public double area() { return Math.PI * radius * radius; } public void scale( double factor ) { radius *= factor; } } Rectangle Similarly, let’s complete the implementation of the class Rectangle . Where would you implement the method area() ? In the class Shape or int the class Rectangle ? Rectangle public class Rectangle extends Shape { private double width; private double height; // ... public double area() { return width * height; } public void scale(double factor) { width = width * factor; height = height * factor; } } Don’t get the wrong impression that inheritance is restricted to the classes that you are defining yourself. Inheritance is often used to specialize existing classes of the Java library. import java.awt.TextField; public class TimeField extends TextField { public Time getTime() { return Time.parseTime( getText() ); } } // java.lang.Object // | // +--java.awt.Component // | // +--java.awt.TextComponent // | // +--java.awt.TextField // | // +--TimeField Polymorphism From the Greek words polus = many and morphˆ e = forms, literally means has many forms. 1. Ad hoc polymorphism (overloading): a method name is associcated with different blocs of code 2. Inclusion (subtyping, data) polymorphism: an identifer (a reference variable) is associated with data of different types with the use of a subtype relation In Java, a variable or a method is polymorphic if it refers to objects of more than one “class/type”. Method overloading Method overloading means that two methods can have the same name but different signatures (the signature consists of the name and formal parameters of a method but not the return value). Constructors are often overloaded, this occurs for the class Shape : Shape() { x = 0.0; y = 0.0; } Shape( int x, int y ) { this.x = x; this.y = y; } ⇒ Method overloading is sometimes referred to as ad hoc polymorphism ( ad hoc = for a specific purpose). Overloading (contd) In Java, some operators are overloaded, consider the “+” which adds two numbers or concatenates two strings, a user can overload a method but not an operator. Since the signatures are different, Java has no problem finding the right method: static int sum( int a, int b, int c ) { return a + b + c; } static int sum( int a, int b ) { return a + b; } static double sum( double a, double b ) { return a + b; } Overloading (contd) The class PrintStream has a specific println method for each primitive type (a...
View Full Document

This note was uploaded on 03/02/2012 for the course ITI 1121 taught by Professor Samaan during the Winter '10 term at University of Ottawa.

Page1 / 51

07Inheritance - ITI 1121. Introduction to Computing II *...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online