Abstracting

 

Abstracting means to ignore or hide everything except essential data that we require for the given problem or situation.

The London tube map is a great example of an abstraction. We don't need info about shops, house addresses or precise directions. We just need station names linked together.

Non-essential information is ignored as we develop an abstraction.

Objectives

Students will be able to:

  • design mulitple class methods with the same name and different parameter lists
  • call those methods appropriately
  • understand the advantages of method overloading

the RoadRacer class

public class RoadRacer{

//first declare the class properties

double average;

//constructor method

public RoadRace(){

this.average = 0;

}

public void addTwo(int time1, int time2){

int total = time1+time2;

this.average = (double)total/2;

}

public void addThree(int time1, int time2, int time3){

int total = time1+time2+time3;

this.average = (double)total/3;

}

public double getAverage(){

return average;

}

}

the RoadRacer class

public class RoadRacer{

//first declare the class properties

double average;

//constructor method

public RoadRace(){

this.average = 0;

}

public void add(int time1, int time2){

int total = time1+time2;

this.average = (double)total/2;

}

public void add(int time1, int time2, int time3){

int total = time1+time2+time3;

this.average = (double)total/3;

}

public double getAverage(){

return average;

}

}

Object Oriented Programming

Method overloading

 

Road racers do time trials. Sometimes they do 2 trials a day, sometimes they do 3.

RoadRacer objects add up time trial times and stores the average time. is the RoadRace class.

Note: there are two methods that really do the same thing, they just have different parameter lists!

Worksheet Task

Complete the worksheet

Method overloading is very convenient when designing methods which have similar behaviour but a different parameter list.

Method overloading is one form of polymorphism - the other is method overriding.

Glossary

method

parameter

cast

return

method overloading

method overriding

polymorphism

Tags

idesyntax file class instantiate constructor type propertymethod