Java Programming: Understanding Polymorphism, Overloading, and Overriding, Study notes of Computer Science

An introduction to programming concepts in java, focusing on polymorphism, method overloading, and method overriding. Polymorphism allows for multiple methods with the same name but different signatures or the same name and signature in an inheritance chain. Overloading refers to methods with different signatures, while overriding involves methods with the same signature. The document also covers the concept of final methods that cannot be overridden and the potential confusion of shadowing variables with the same name in a subclass.

Typology: Study notes

Pre 2010

Uploaded on 03/28/2010

koofers-user-ck9-1
koofers-user-ck9-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Introduction to Programming
with Java, for Beginners
Polymorphism
- Overloading
- Overriding
More overriding (inheritance)
CIS110 1
Polymorphism
Polymorphism means many (poly) shapes (morph)
In Java, polymorphism refers to the fact that you can have
multiple methods with the same name in the same class
There are two kinds of polymorphism:
Overloading
¾Two or more methods with different signatures
Overriding
¾A method in a subclass to “override” a method in the superclass
that has the same signature
We’ve already seen Overloading scenario with Constructors
E.g. public RealVector(double x, double y) {..}
public RealVector(double mag, Angle theta) {..}
CIS110 2
Method Overloading
Method overloading occurs when
A class has two or more methods with the same name but
different signatures
¾Different signature -> the number, order, or types of their
parameters differ
// the foo method is overloaded
public void foo() {.. }
public void foo(int x) { ..}
public void foo(int x, double y) {..}
When the foo(..) method is called, Java picks the one that
“matches”. E.g.
foo(10, 350.5);
CIS110 3
Overriding
Overriding occurs if
There are two or more methods with the same
name and the same signature in an inheritance
chain
For example, the Object class has a toString()
method
¾It can be overriden in a subclass simply by creating a
method with the same signature
public String toString() {. .}
Java picks the “lowest” method in the inheritance
chain possible
pf2

Partial preview of the text

Download Java Programming: Understanding Polymorphism, Overloading, and Overriding and more Study notes Computer Science in PDF only on Docsity!

Introduction to Programming

with Java, for Beginners

Polymorphism- Overloading- OverridingMore overriding (inheritance)

CIS

1

Polymorphism

„

Polymorphism means

many

(poly)

shapes

(morph)

„

In Java, polymorphism refers to the fact that you can havemultiple methods with the same name in the same class „

There are two kinds of polymorphism:

„

Overloading

¾

Two or more methods with

different signatures

„

Overriding

¾

A method in a subclass to “override” a method in the superclassthat has the

same signature

„

We’ve already seen Overloading scenario with Constructors

E.g. public RealVector(double x, double y) {..}

public RealVector(double mag, Angle theta) {..}

CIS

2

Method Overloading

Method

overloading

occurs when

„

A class has two or more methods with the same

name

but

different

signatures

¾

Different signature -> the number, order, or types of theirparameters differ

// the foo method is overloadedpublic void foo() {.. }public void foo(int x) { ..}public void foo(int x, double y) {..}

„

When the foo(..) method is called, Java picks the one that

“matches”. E.g.

foo(10, 350.5);

CIS

3

Overriding

„

Overriding

occurs if

„

There are two or more methods with the samename

and the same signature

in an inheritance

chain

„

For example, the Object class has a toString()method

¾

It can be

overriden

in a subclass simply by creating a

method with the same signature

public String toString() {. .}

„

Java picks the “lowest” method in the inheritancechain possible

CIS

4

Some Methods cannot be overwritten

class Animal {

final boolean canMove(int direction) { ... }

} class Rabbit extends Animal {

// inherits but cannot override canMove(int)

„

Just like variables can be final, methods canalso be final

„

Methods that are final cannot be overridden inthe subclass

CIS

5

Overriding Variables

„

You can, but you shouldn't

„

Possible for child class to declare variable withsame name as variable inherited from parentclass

„

One in child class is called shadow variable

¾

It shadows the variable with same name in the parent class

„

Confuses everyone!

„

Child class already can gain access to inheritedvariable with same name

„

There's no good reason to declare new variable withthe same name

CIS

6

Example

class Animal {

protected String type;public Animal(){

type = "Animal"; }

Dog d = new Dog();> d.type //can field as not set privateDog

public class Dog extends Animal{

String type;public Dog(){

type = "Dog";

} }

CIS

7

Some Variables Cannot be Shadowed „

class BorderLayout {

public static final String NORTH = "North";

„

If you were to create a subclass ofBorderLayout, you would not be able toredefine NORTH