• Tidak ada hasil yang ditemukan

JAVA PROGRAMMING II

N/A
N/A
Protected

Academic year: 2025

Membagikan "JAVA PROGRAMMING II"

Copied!
7
0
0

Teks penuh

(1)

JAVA PROGRAMMING II

CPCS 203

polymorhism.ppt 1

(2)

Polymorphism

(3)

David Matuszek-upenn polymorhism.ppt 3

superclass construction I

• the very first thing any constructor does, automatically, is call the default constructor for its superclass

• class Foo extends Bar { Foo() { // constructor

super(); // invisible call to superclass constructor ...

• you can replace this with a call to a specific superclass constructor

• use the keyword super

• this must be the very first thing the constructor does

• class Foo extends Bar {

Foo(String name) { // constructor

super(name, 5); // explicit call to superclass constructor ...

(4)

superclass construction II

Unless you specify otherwise, every constructor calls the default constructor for its superclass

class Foo extends Bar { Foo() { // constructor

super(); // invisible call to superclass constructor ...

You can use this(...) to call another constructor in the same class:

class Foo extends Bar {

Foo(String message) { // constructor

this(message, 0, 0); // your explicit call to another constructor ...

You can use super(...) to call a specific superclass constructor

class Foo extends Bar {

Foo(String name) { // constructor

super(name, 5); // your explicit call to some superclass constructor ...

Since the call to another constructor must be the very first thing you do in the

(5)

David Matuszek-upenn polymorhism.ppt 5

shadowing

• This is called shadowing—name in class Dog shadows name in class Animal

class Animal {

String name = "Animal";

public static void main(String args[]) { Animal animal = new Animal();

Dog dog = new Dog();

System.out.println(animal.name + " " + dog.name);

} }

public class Dog extends Animal { String name = "Dog";

}

Animal Dog

(6)

overriding

• this is called overriding a method

• method print in Dog overrides method print in Animal

• a subclass variable can shadow a superclass variable, but a subclass method can override a superclass method

class Animal {

public static void main(String args[]) { Animal animal = new Animal();

Dog dog = new Dog();

animal.print();

dog.print();

}

void print() {

System.out.println("Superclass Animal");

} }

public class Dog extends Animal { void print() {

(7)

David Matuszek-upenn polymorhism.ppt 7

how to override a method

• create a method in a subclass having the same signature as a method in a superclass

• that is, create a method in a subclass having the same name and the same number and types of parameters

• parameter names don’t matter, just their types

• restrictions:

• the return type must be the same

• the overriding method cannot be more private than the method it overrides

Referensi

Dokumen terkait

Furthermore, using such a static factory method mandates that the client refer to the returned object by its interface rather than by its implementation class, which is generally

It goes without saying that you need to create your own animation class, a subclass of Animation , that performs some change to the color of a sprite over time, as the examples

A lambda expression must match the argument types and return type in the signature of the single abstract method in the interface2. This is called being compatible with the

A class, in the context of Java, are templates that are used to create objects, and to define object data types and methods.. For

To create object code, the computer uses a special software tool called a

• If you use this method, the SimpleApplet source file looks like this: /* */ public class SimpleApplet extends Applet { …} • With this approach, you can quickly iterate through

Keyword What It Does abstract Indicates that the details of a class, a method, or an interface are given elsewhere in the code.. assert Tests the truth of a condition that the

An interface can have methods and variables just like the class but the methods declared in interface are by default abstract only method signature, no body.. The variables declared in