COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad
Slides by David A. Gaitros Overview Overloading Extending Classes Hiding Fields and Class Methods Slides by David A. Gaitros
Slides by David A. Gaitros Overloading Overloading refers to the ability to allow different methods or constructors of a class to share the same name. The name is said to be overloaded with multiple implementations. Commonly know as the feature of polymorphism in the object oriented paradigm. Slides by David A. Gaitros
Slides by David A. Gaitros Overloading The language can tell if two methods with the same name are different by the “signature”. The signature consists of the name of the method and the data type of the parameters. Note the return type, name of parameters, and final designations of parameters are not part of the signature. Slides by David A. Gaitros
Slides by David A. Gaitros Overloading Rules for overloading: Two methods in the same class my have the same name provided that they: Have different number of parameters, or Same number of parameters but of different type. Slides by David A. Gaitros
Slides by David A. Gaitros Overloading Example: Public class Point{ protected double x,y; public Point(){ x=0.0; y=0.0; } public double distance (Point other){ double dx = this.x – other.x; double dy = this.y – other.y; return Math.sqrt(dx * dy + dy * dy); Slides by David A. Gaitros
Slides by David A. Gaitros Overloading public double distance (double x, double y) { double dx = this.x –x; double dy = this.y – y; return Math.sqrt(dx * dx + dy * dy); } public double distance (int x, int y) { double dx = this.x – (double) x; double dy = this.y – (double) y; return Math.sqrt (dx * dx + dy *dy); public double distance () { return Math.sqrt (x*x + y *y); Slides by David A. Gaitros
Slides by David A. Gaitros Overloading Two situations to overload When there is a general, nondiscriminative description of the functionality that fits all the overloaded methods. When all the overloaded methods offer the same functionality, with some of them providing default arguments. Slides by David A. Gaitros
Slides by David A. Gaitros Extending Classes Inheritance (example) – When class Hourly_employee inherits from, or extends the class Employee, then Hourly_employee is called a subclass of the class Employee. In Java, each class my only inherit properties from another class. Slides by David A. Gaitros
Slides by David A. Gaitros Extending Classes Syntax of the extend clause: [ClassModifiers] class ClassName [extends SuperClass] [implements Interace1, Interface2, etc] { ClassMemberDeclarations } Slides by David A. Gaitros
Slides by David A. Gaitros Extending Classes import java.awt.Color; public class ColoredPoint extends Point { public Color color; public ColorPoint (final double x, final double y, final Color color) { super (x,y); this.color = color; } public ColoredPoint (final double x, final double y) { this (x,y, Color.black); // default color public ColoredPoint () { color = Color.black; Slides by David A. Gaitros
Slides by David A. Gaitros Extending Classes Note in the constructor. The invocation of super (…) must be the first statement in the constructor of the extended class Note: The second constructor of the ColoredPoint class supplies a default value for the color field. Note: The third constructor has not explicit invocation using super or this is made. This is also an example of overloading. Slides by David A. Gaitros
Slides by David A. Gaitros Extending Classes Subtype Subtypes are a specialized version of a specific type. For instance, let us say that you are storing integers for test scores. Since your test scores do not go below 0 or above 100, you could have a specialized subtype of integers that only had a valid range of 0 to 100. Slides by David A. Gaitros
Slides by David A. Gaitros Extending Classes Overriding Overriding is different from overloading. In overriding, methods share the same name, signature, and return type in contrast to overloading. class A { public void m1() { … method .. } public void m1( int i) { … method . .} // Note class A contains two overaloaded methods. Slides by David A. Gaitros
Slides by David A. Gaitros Extending Classes Now assumer we have the two classes: class B { public void m2() { . . } } class C extends B { public coid m2 () { . . } B b = new B(); C c = new C(); b.m2(); // invoke the m2() in class B c.m2(); // invoke the m2() in class C Slides by David A. Gaitros
Hiding Fields and Class Methods Hiding refers to the introduction of a field or a class method in a subclass that has the same name as a field or class method in a superclass. Overriding and hiding are different concepts Instance methods can only be overriden. Class methods and fields can only be hiden. Slides by David A. Gaitros
Hiding Fields and Class Methods Example: public class A { int x; void y() { … method . . } static void z() { … method . .} } public class B extends A { float x; // hiding void y () { … method . .} // overriding static int z () { … method . . } // hiding Slides by David A. Gaitros
Hiding Fields and Class Methods There is a crucial distinction between overriding and hiding. When an overridden method is invoked, the implementation that will be executed is chosen at run time. When the hidden method or field is invoked or accessed, the copy that will be used is determined at compile time. Class methods and fields are statically bound. Slides by David A. Gaitros