COP 3331 Object Oriented Analysis and Design Chapter
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad
Overview l l l Overloading Extending Classes Hiding Fields and Class Methods
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.
Overloading l 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.
Overloading l Rules for overloading: Two methods in the same class my have the same name provided that they: 1. 2. Have different number of parameters, or Same number of parameters but of different type.
Overloading l 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); }
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); } }
Overloading l 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.
Extending Classes l 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.
Extending Classes l Syntax of the extend clause: [Class. Modifiers] class Class. Name [extends Super. Class] [implements Interace 1, Interface 2, etc] { Class. Member. Declarations }
Extending Classes import java. awt. Color; public class Colored. Point extends Point { public Color color; public Color. Point (final double x, final double y, final Color color) { super (x, y); this. color = color; } public Colored. Point (final double x, final double y) { this (x, y, Color. black); // default color } public Colored. Point () { color = Color. black; } }
Extending Classes l l 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 Colored. Point 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.
Extending Classes l 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.
Extending Classes l 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 m 1() { … method. . } public void m 1( int i) { … method. . } // Note class A contains two overaloaded methods.
Extending Classes l Now assumer we have the two classes: class B { public void m 2() {. . } } class C extends B { public coid m 2 () {. . } } B b = new B(); C c = new C(); b. m 2(); // invoke the m 2() in class B c. m 2(); // invoke the m 2() in class C
Hiding Fields and Class Methods l l 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.
Hiding Fields and Class Methods l 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
Hiding Fields and Class Methods l 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: 18