• Tidak ada hasil yang ditemukan

JAVA PROGRAMMING II

N/A
N/A
Protected

Academic year: 2025

Membagikan "JAVA PROGRAMMING II"

Copied!
14
0
0

Teks penuh

(1)

JAVA PROGRAMMING II

CPCS 203

(2)

Polymorphism

(3)

signatures

• in any programming language, a signature is what distinguishes one function or method from another

• in C, every function has to have a different name

• in Java, two methods have to differ in their names, or in the number or types of their parameters

• foo(int i) and foo(int i, int j) are different

• foo(int i) and foo(int k) are the same

• foo(int i, double d) and foo(double d, int i) are different

• in C++, the signature also includes the return type

• but not in Java!

(4)

poly + morphism

• poly is Latin for “many”; morph is Latin for “form”

• in Java, a method or constructor may have multiple signatures

• this is called overloading

• EXAMPLE:

Cat c1 = new Cat();

Cat c2 = new Cat(“Felix”); // overloaded constructor c1.speak();

c1.speak(3); // overloaded method

• overloading permits multiple ways to construct an

object, or multiple ways to call a method

(5)

polymorphism in inheritance

• a method or constructor may be modified in a subclass during inheritance

• this is called overriding

• EXAMPLE:

// assuming both Cat and Dog extend Animal, and Animal has // a speak() method defined:

Cat c = new Cat();

c.speak(); // overridden to speak specifically like a Cat Dog d = new Dog();

d.speak(); // overridden to speak specifically like a Dog

• if you override a superclass variable, you may get unintended consequences

• this is called shadowing

• it is generally to be avoided

(6)

an example of overloading

class Test {

public static void main(String args[]) { myPrint(5);

myPrint(5.0);

}

static void myPrint(int i) {

System.out.println("int i = " + i);

}

static void myPrint(double d) { // same name, different parameters System.out.println("double d = " + d);

} }

int i = 5

double d = 5.0

(7)

DRY (Don’t Repeat Yourself)

• when you overload a method, only one of the similar methods should do most of the work:

void debug() {

System.out.println("first = " + first + ", last = " + last);

for (int i = first; i <= last; i++) {

System.out.print(dictionary[i] + " ");

}

System.out.println();

}

void debug(String s) {

System.out.println("At checkpoint " + s + ":");

debug();

}

(8)

another reason for polymorphism

• you may want to do “the same thing” with different kinds of data:

• class Student extends Person { ...

void printInformation() {

printPersonalInformation();

printGrades();

} }

• class Professor extends Person() { ...

void printInformation() { printPersonalInformation();

printResearchInterests();

} }

• Java’s print and println methods are heavily overloaded

• if Person has a method called “printInformation”, it is overridden

• if Person has no such method, it’s still polymorphism—2 different implementations (in different classes) that bear the same name

(9)

legal assignments

• widening a number is legal

• narrowing a number is illegal (unless you cast) class Test {

public static void main(String args[]) { double d;

int i;

d = 5; // legal i = 3.5; // illegal i = (int) 3.5; // legal }

}

(10)

legal method calls

• legal because parameter transmission is equivalent to assignment

• myPrint(5) is like double d = 5; System.out.println(d);

class Test {

public static void main(String args[]) { myPrint(5);

}

static void myPrint(double d) { System.out.println(d);

}

}

5.0

(11)

illegal method calls

• illegal because parameter transmission is equivalent to assignment

• myPrint(5.0) is like int i = 5.0; System.out.println(i);

class Test {

public static void main(String args[]) { myPrint(5.0);

}

static void myPrint(int i) { System.out.println(i);

} }

myPrint(int) in Test cannot be applied to (double)

(12)

Java uses the most specific method

• class Test {

public static void main(String args[]) { myPrint(5);

myPrint(5.0);

}

• static void myPrint(double d) {

System.out.println("double: " + d);

}

• static void myPrint(int i) {

System.out.println("int: " + i);

} }

• int:5

double: 5.0

(13)

multiple constructors I

• You can “overload” constructors as well as methods:

• Counter() { count = 0;

}

Counter(int start) { count = start;

}

(14)

multiple constructors II

• one constructor can “call” another constructor in the same class, but there are special rules

• you call the other constructor with the keyword this

• the call must be the very first thing the constructor does

• Point(int x, int y) { this.x = x;

this.y = y;

sum = x + y;

}

• Point() {

this(0, 0);

}

• a common reason for overloading constructors is (as above) to provide default values for missing parameters

Referensi

Dokumen terkait

This abstract class is customized through inheritance to use a stack for storing possible moves (i.e., the array path) for depth first search and a queue for breadth first search..

He helps to organize the London Java Community and represents them on the Java Community Process Executive Committee, where he works to define new standards for the Java ecosystem.

As a second example, the Java compiler javac transforms a .java source file into a .class file that is written in Java bytecode , which is the machine language for an

Table 1.Types of feed plants for West Java black langurs in the TSI Bogor forest area Natural feed local name Latin Name Family Information eaten part Kayumanis Cinnamomum

A testing tool, named JaBUTi Java Bytecode Understanding and Testing, which supports the application of such criteria for testing Java programs and components, is used for our

Foreground and Background Colors • To set the background color of an applet’s window, use setBackground • To set the foreground color the color in which text is shown, for example,

it is felt in remote areas that there is no selection and coaching of futsal sports for the West Java area only focusing on urban areas in West Java, 3 The relevant result

Bale Kambang is a popular tourist destination in East Java, Indonesia, known for its beautiful beach, rocky islands, and historical