Docsity

Java Programming: Understanding Overriding, Polymorphism, and Method Overloading, Slides of Computer science

Partial preview of the text

Download Java Programming: Understanding Overriding, Polymorphism, and Method Overloading and more Slides Computer science in PDF only on Docsity!

ICS 111

Overriding, Polymorphism

overloading methods

overriding methods

object polymorphism

abstract methods, abstract classes

final methods, final classes

example from textbook: bank accounts

Method Overloading

● We have already seen cases where methods with the same name are called on

different parameters:

  • (^) System.out.println() and System.out.println(String s)
  • (^) constructors with different parameter types

● In each case, these are different methods that just happen to have the same name

● This is called overloading

  • (^) for example, System.out.println is an overloaded method
  • (^) constructors can be overloaded

● Overloading is fine under two conditions:

  • (^) it doesn’t cause confusion to callers of the methods ● (^) i.e. the methods should do “the same thing”, even though on different parameters
  • (^) it is done on purpose, rather than accidentally ● (^) accidental overloading can happen when overriding methods – discussed on the next slide

Overriding and overloading:

remembering the difference

● Overriding is when a subclass

re-implements the method of a

superclass

  • (^) the new method overrides (takes over from) the corresponding method in the superclass

● Overloading is when the same

name refers to different

methods

  • (^) the name is overloaded because, instead of referring to a single method, it refers to several different methods ● (^) the same name has to “carry” multiple methods – it is overloaded

What method gets called?

● Suppose we have a method that takes as parameter an Object and prints it:

public static void printObject(Object x) { String s = x.toString(); System.out.printf("%s\n", s); }

● if I have a variable of type ArrayList al = …

● and given that ArrayList overrides the toString() method of its superclass

● calling printObject(al) calls which toString method?

  • (^) Object.toString(), or
  • (^) ArrayList.toString()

● calling the method in ArrayList is more useful

● and this is what Java does:

  • (^) method calls are determined dynamically by the actual underlying object, not by the type declaration

things to be careful about

● remember to use super when calling methods from

the superclass

– this and super help resolve name clashes

● use accessor and mutator methods to access the

private variables in the superclass

●

constructor calls to this() or super() must be the

first statement in the body of a constructor

● this refers to the actual object, not the declared

object type

– this.toString() calls the toString() method of the

subclass, not of the superclass, nor Object.toString()

abstract classes and methods

● (^) sometimes a class is designed to be subclassed ● (^) the designer of the superclass may want to require the subclass to provide a specific method ● (^) this method is called abstract in the superclass

  • (^) and does not have an implementation in the superclass public abstract String concatenate(String s); ● (^) any class with one or more abstract methods is an abstract class
  • (^) and must be declared with the keyword abstract public abstract class StringOperations { …. ● (^) abstract classes have no constructors ● (^) we cannot create an object of an abstract class ● (^) but we can have variable and parameter types be abstract classes public class Example extends StringOperations { … } StringOperations s1 = new Example(); ● (^) summary: an abstract class forces implementers of subclasses to implement all the methods that are abstract in the superclass ● (^) implementers of subclasses still inherit any non-abstract methods from the superclass

Worked-out example:

Bank Account class

● from textbook Section 9.4, How-To 9.

● design and implement a class hierarchy to represent different

types of bank account

●

at the root of the hierarchy is a BankAccount object that can

represent any account

  • (^) it keeps the balance in an instance variable
  • (^) it has a getBalance() accessor method
  • (^) it has mutator methods for deposits and withdrawals
  • (^) it has a method to do end-of-month processing

● which doesn’t do anything

● but may be overridden by subclasses

Worked-out example: subclasses

● (^) each subclass of BankAccount, e.g SavingsAccount and CheckingAccount, provides the deposit, withdrawal, getBalance, and monthEnd methods

  • (^) only overriding whatever methods it needs to override
  • (^) we could easily have an account type that does not override any methods ● (^) the SavingsAccount overrides the monthEnd method to deposit interest into the bank account once a month:

double interest;

public void monthEnd() {

super.deposit(interest * super.getBalance());

the book handles a few more cases, specifically computing the interest on the

minimum rather than the final balance

● (^) in this example, both uses of super are optional, since SavingsAccount does not override getBalance and deposit

Summary

●

we are starting to see that programming

with objects is more than just getting our

code to work: it is also about

representing our data in clear and useful

ways

●

once we have created data

representations useful for the task at

hand, the actual code can be relatively

simple

●

coding includes the coding of methods

inside a class, and the coding of methods

that create and use objects