48
Jan. 2004 1 Object Oriented Programmin Yangjun Chen Dept. Business Computing University of Winnipeg

Jan. 20041 Object Oriented Programming Yangjun Chen Dept. Business Computing University of Winnipeg

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Jan. 2004 1

Object Oriented Programming

Yangjun Chen

Dept. Business ComputingUniversity of Winnipeg

Jan. 2004 2

Outline: OOP

• What is OOP?• Class, instance, field, method, ...• “this” key word• Method Overloading• Inheritance• Method Overriding• Abstract and Interface

Jan. 2004 3

What is OOP?

• Procedural programming is where you would try to solve a problem using pre-determined types: int, floats, strings and arrays.

• In OOP, you create a model that best represents the problem.• The programmer defined model is known a class.• A class is a programmer defined type, together with a lot of

procedures manipulating over it. Such a type can be used as template for instance of the class.

Jan. 2004 4

What is OOP?

• A class has two parts:- fields: describe what a class is- methods: describe what a class does

• Using the template of a class, any number of objects may be created, each of these objects is called an instance of the class.

• Different objects of the same class have the same fields and methods, but the values of the fields will generally differ.

• For example, all books have the page number, but the page number can be different from each other.

Jan. 2004 5

Class Car

class Car {String licencePlate;double speed;double maxSpeed;

}• The variables licencePlate, speed, and maxSpeed are called

member variables, instance variables, or fields of the class.

Jan. 2004 6

Creating Objects

• To create a new object or instantiate an object, we use the new operator:

Car c;c = new Car( );

• We first declare a variable c of Car class and then instantiate it by calling the class’s constructor Car().

• This can be done in a single line as follows:Car c = new Car( );

Jan. 2004 7

Accessing Fields

• Once an object is created, the “.” Operator can be used to access fields in it.

• For example:Car c = new Car( );c.licensePlate = “BGT 810”;c.speed = 60.0;c.maxSpeed = 100.0;System.out.println(c.licensePlate + “ is moving at” + c.speed +

“km/h.”);

Jan. 2004 8

Using Object within a Class

class CarTest {public static void main(String args[]){

Car c = new Car( );c.licensePlate = “BGT 810”;c.speed = 60.0;c.maxSpeed = 100.0;System.out.println(c.licensePlate + “ is moving at” + c.speed +

“km/h.”);} //main

}

Jan. 2004 9

Using Object within a Class

• This program needs both the CarTest and Car class to run.• To make them work properly, both files should be in the same

directory.• To run CarTest:

- javac Car.java- javac CarTest.java- java CarTest

• Note that Car does not have a main( ) method and can only exists when called by other programs with a main( ) method.

Jan. 2004 10

Methods

• Data types are pretty useless unless you can do something with them.

• Classes can contain many methods that do many different things.

Class Car {String licensePlate;double speed, maxSpeed;void accToMax( ){

this.speed = this.maxSpeed;}//accToMax

}//class Car

Jan. 2004 11

Methods

• The Car class is the same with the exception of a method accToMax( ).- What does the void keyword tell us?

• Inside the method, there is only one statement- this.speed = this.maxSpeed;

• Notice that the Car class variables are prefixed with the keyword this to indicate that we are referring to the fields in the current objet.

Jan. 2004 12

class CarTestYetAgain {public static void main(String args[]){

Car c = new Car( );c.licensePlate = “BGT 810”; c.speed = 60; c.maxSpeed = 100.0;System.out.println(c.licensePlate + “is moving at “ + c.speed +

“ km/h.”);c.accToMax( );System.out.println(c.licensePlate + “is moving at “ + c.speed +

“ km/h.”);} //main

} //class

Invoking Instance Methods

Jan. 2004 13

Invoking Methods

• The output that will be generated is:- BGT 810 is moving at 60 km/h.- BGT 810 is moving at 100 km/h.

• The accToMax( ) method is completely enclosed within the Car class. In Java, every method must belong to a class.

• This is different from C++, where methods can be placed outside of classes.

Jan. 2004 14

What is the “this”?

• In the body of a method definition, there could be times when you want to refer to the current object - the object in which the method is contained. Or maybe you want to refer to the object’s instance variables or pass the current object as an argument to another method.

• Use the “this” keyword to refer to the above.• The “this” keyword can be used anywhere the current object

could appear - in dot notation to refer to the object instance variables, as an argument, as a return value, etc.

Jan. 2004 15

“this” Keyword

y = this.x; //the x instance variable for the current obejctthis.myMethod(this); //call myMethod( )

//defined in this//class passing it the current

objectreturn this; //return the current object

• In many cases, it is possible to omit the “this” keyword entirely.

Jan. 2004 16

Implied “this”?

• You can refer to both instance variables and methods that are defined in the current class by their respective names. The “this” keyword is not needed because it is implicit for those references.

• Omitting the “this” keyword for instance variables depends on whether there are variables with the same name declared in the local scope. If there are, then the “this” keyword must be used to refer to the instance variables of the class to avoid “name clash”.

Jan. 2004 17

Implied “this”?

class Car {String licensePlate;double speed, maxSpeed;

void accToMax( ){speed = maxSpeed; //leave out “this”

} //accToMax} //class Car

Jan. 2004 18

“this” Keyword

• Note that since “this” is a reference to the current instance of the class, it should only be used inside the body of an instance method definition.

• So class methods - methods that have the keyword static in their declaration - cannot use “this’.

Jan. 2004 19

Passing Arguments to Methods

• Let’s allow Car objects to change the speed of a car by calling a method.void accelerate(double delta){

this.speed = this.speed + delta;if (this.speed > this.maxSpeed){

this.speed = this.maxSpeed;}

if (this.speed < 0.0) {this.speed = 0.0}

} //end of accelerate

Jan. 2004 20

Passing Arguments to Methods

• What is the first line of the method called?• Accelerate( ) returns no value and accepts a double referred to

as delta.• Method arguments in Java are passed in by value, or by

reference.- If the data type of an argument is primitive, the argument is passed by value.- If the data type of argument is a class, the argument is

passed by reference.

Jan. 2004 21

class Car {String lecensePlate;double speed, maxSpeed;

void accToMax( ) {this.speed = this.maxSpeed;} //accToMax

void accelerate(double delta){this.speed = this.speed + delta;if (this.speed > this.maxSpeed){

this.speed = this.maxSpeed;}if (this.speed < 0.0) {

this.speed = 0.0}

} //end of accelerate} //class Car

Jan. 2004 22

class CarTestYetAgain {public static void main(String args[]){

Car c = new Car( );c.licensePlate = “BGT 810”; c.speed = 0.0;c.maxSpeed = 100.0;System.out.println(c.licensePlate + “is moving at “ + c.speed

+ “ km/h.”);for (int i = 0; i < 15; i++)

c.accelerate(10.0);System.out.println(c.licensePlate + “is moving at “ +

c.speed + “ km/h.”);} //main

} //class

Class CarTestYetAgain

Jan. 2004 23

Returning Values from Methods

• Using the return keyword at the end of a method and declaring the type to be returned in the method’s signature.

• Let’s write a method to retrieve the license:String getLicensePlate( ) {

return this.licensePlate;} //end of getLicensePlate

• Methods of this type are referred to as accessor methods because they merely access the fields and then exit.

Jan. 2004 24

Method Overloading

• Overloading is when the same method or operator can be used on different types of data. For example:- the “+” operator:

int a;a = a +1;System.out.println(c.licensePlate + “is moving at “ + c.speed + “

km/h.”);• To create overloaded method, simply write two methods with the

same name but with different argument list.• Which method gets called depends on the signature which is the

method name, number, type and order of the arguments passed into the method.

Jan. 2004 25

“this” in Constructors (Example for Method Overloading)

• To invoke a constructor from another constructor in the same class:class Car { String licensePlate; double speed, maxSpeed;

public Car(String licensePlate, double speed, double maxSpeed) {this.licensePalte = licensePlate;this.speed = speed;this.maxSpeed = maxSpeed;

}public Car(String licensePlate, double maxSpeed) {

this(licensePlate, 0.0, maxSpeed); }void accTomax ( ) { … } void accelerate ( ) {…}

}• This approach saves several lines of code and also allows

easier modification of the code.

Jan. 2004 26

Inheritance

• The ability to reuse code is one of the main advantages in OOP languages over non-OOP languages.

• By inheriting from another class, an object inherits the variables and methods from that class. It can keep those it wants and replace those it doesn’t want.

• To inherit from a class, use the extends keyword:public class Compact extends Car {

int x = 0;int y = 0;

}

Jan. 2004 27

Inheritancepublic class Compact {

int x = 0;int y = 0;String licensePlate; double speed, maxSpeed;public Car(String licensePlate, double speed, double

maxSpeed) {this.licensePalte = licensePlate;this.speed = speed;this.maxSpeed = maxSpeed;

}public Car(String licensePlate, double maxSpeed) {

this(licensePlate, 0.0, maxSpeed); }void accTomax ( ) { … } void accelerate ( ) {…}

}

Jan. 2004 28

Multilevel Inheritance

• The concept of inheritance can be extended further to include multiple levels.

• For example, if we had a class called MotorVehicle, we could have the following:

public class Car extends MotorVehicle {… ...

}

public class Compact extends Car {… ...

}• Multilevel inheritance increases complexity.

Jan. 2004 29

Multiple Inheritance

• Do not confuse multilevel inheritance with multiple inheritance.

• In C++, a class is allowed to inherit from more than one class.• Java does not allow multiple inheritance.• However, Java does have what are known as interfaces that

can be used to accomplish the same kind of tasks as multiple inheritance would.

Jan. 2004 30

Method Overriding

• Method overriding is creating a method in the subclass that is already defined in its superclass.

• This allows programmers to hide methods in the superclass that are not needed or that need to be implemented differently.

• It also allows for more efficient reuse of code.

public class Compact extends Car {int x = 0;int y = 0; void accToMax( ){

speed = 20 + maxSpeed;} //accToMax + 20

}

Jan. 2004 31

public class Compact { int x = 0;int y = 0;String licensePlate; double speed, maxSpeed;public Car((String licensePlate, double speed, double maxSpeed) {

… ...}public Car(String licensePlate, double maxSpeed) {

this(licensePlate, 0.0, maxSpeed); }void accToMax( ){

speed = 20 + maxSpeed;} //accToMax + 20

void accelerate ( ) {…}}

Method Overriding

Jan. 2004 32

Abstract

• Abstract classes created using the abstract keyword:public abstract class MotorVehicle { … }

• In an abstract class, several abstract methods are declared.- An abstract method is not implemented in the class, only declared. The body of the method is then implemented in subclass.- An abstract method is decorated with an extra “abstract” keyword.

• Abstract classes can not be instantiated! So the following is illegal:

MotorVehicle m = new MotorVehicle;

Jan. 2004 33

Abstract

• Abstract methods are declared but do not contain an implementation.

• For example, the MotorVehicle class may have an abstract method gas( ):

public abstract class MotorVehicle {double speed, maxSpeed;void accToMax( ) { speed = maxSpeed;}public abstract void gas( );…

}• So class Car (assume that Car is a subclass of MotorVehicle)

would override this abstract method and provide an implementation for gas( ).

Jan. 2004 34

Abstract

public class Car extends MotorVehicle {… ...public abstract void gas( ) { … };…

}

MotorVehicle

Car

Jan. 2004 35

Interface

• An interface is like a class with nothing but abstract methods and final and static fields (constants).

• Interface can be added to a class that is already a subclass of another class.

• To declare an interface:public interface ImportTax {

public double calculateTax( );}

Jan. 2004 36

Interface

• (Abstract) classes are extended but interfaces are implemented.public class Compact extends Car

implements ImportTax { … ...}

• An interface can also have fields, but the fields must be declared as final and static.

• We’ll use interface later on.

Jan. 2004 37

Interface

import java.util.*;

interface CanFight { void fight ( );}interface CanSwim { void swim ( );}interface CanFly {void fly ( );}

class ActionCharacter { public void fight( ) { }}

class Hero extends ActionCharacterimplements CanFight, CanSwim, CanFly {public void fight ( ) { }public void swim ( ) { }public void fly ( ) { }

}

Jan. 2004 38

Interface

public class Adventure {static void t(CanFight x) { x.fight();}static void u(CanSwim x) { x.swim();}static void v(CanFly x) { x.fly();}static void w(ActionCharacter x) { x.fight();}public static void main (String[ ] args) {

Hero h = new Hero( );t(h); //Treat it as a CanFightu(h); //Treat it as a CanSwimv(h); //Treat it as a CanFlyw(h); //Treat it as an ActionCharacter

} }

Jan. 2004 39

Difference between Abstract Class and Interface

• An abstract class is a class containing several abstract methods.Each abstract method is prefixed with the keyword “abstract”.

• An abstract class can not be instantiated but can be extended (subclassed).

• An interface contains only abstract methods and constants. Each abstract method is not prefixed with the keyword “abstract”.

• An interface can only be implemented.

Jan. 2004 40

Interface

import java.util.*;import java.lang.*;

interface CanFight { void fight ( );}interface CanSwim { void swim ( );}interface CanFly {void fly ( );}

class ActionCharacter { public void fight( ) { }}class Hero extends ActionCharacter

implements CanFight, CanSwim, CanFly {public void fight ( ) {System.out.println(“Can fight!”);}public void swim ( ) {System.out.println(“Can swim!”); }public void fly ( ) {System.out.println(“Can fly!”);}

}

Jan. 2004 41

Interface

public class Adventure {static void t(CanFight x) { x.fight();}static void u(CanSwim x) { x.swim();}static void v(CanFly x) { x.fly();}static void w(ActionCharacter x) { x.fight();}public static void main (String[ ] args) {

Hero h = new Hero( );t(h); //Treat it as a CanFightu(h); //Treat it as a CanSwimv(h); //Treat it as a CanFlyw(h); //Treat it as an ActionCharacter

} }

Jan. 2004 42

Hero

ActionCharacter CanFight CanSwim CanFly

subclass implementation

h

instantiation

Hero h = new Hero( )t(h); //Treat it as a CanFightu(h); //Treat it as a CanSwimv(h); //Treat it as a CanFlyw(h); //Treat it as an ActionCharacter

static void t(CanFight x) { x.fight();}static void u(CanSwim x) { x.swim();}static void v(CanFly x) { x.fly();}static void w(ActionCharacter x) { x.fight();}

Jan. 2004 43

Upcasting and Polymorphism

• Upcasting: Taking an object reference and treating it as a reference to its base type is called upcasting, because of the way inheritance trees are drawn with the base class at the top.

class Note { private int value; private Note(int val) {value = val;} public static final Note

middle_c = new Note(0),c_sharp = new Note(1),b_flat = new Note(2);

}

Jan. 2004 44

Upcasting and Polymorphism

class Instrument { public void play(Note n) {

System.out.println(“Instrument.play()”) }}

class Wind extends Instrument { public void play(Note n) {

System.out.println(“Wind.play()”); }}

Instrument

Wind

Jan. 2004 45

Upcasting and Polymorphism

public class Music { public static void tune(Instrument i) {

// …i.play(Note.middle_c);

} public static void main(String[] args) {

Wind flute = new Wind();tune(flute); //Upcasting

}}

Jan. 2004 46

Upcasting and Polymorphism

• Polymorphism: In Java, the principle that the actual type of the object determines the method to be called is called polymorphism.class Shape { void draw() {} void erase() {}}

class Circle extends Shape { void draw() {

System.out.println(“Circle.draw()”); } void erase() {System.out.println(“Circle.erase()”);}}

Shape

draw()erase()

Squaredraw()erase()

Triangledraw()erase()

Circledraw()erase()

Jan. 2004 47

class Square extends Shape { void draw() {

System.out.println(“Square.draw()”); } void erase() {System.out.println(“Square.erase()”);}}

class Triangle extends Shape { void draw() {

System.out.println(“Triangle.draw()”); } void erase() {System.out.println(“Triangle.erase()”);}}

Upcasting and Polymorphism

Jan. 2004 48

public class Shapes { public static Shape randShape() {

switch((int) (Math.random()*3)) {case 0: return new Circle();case 1: return new Square();case 2: return new Triangle();default : return new Circle();}}

public static void main(String[] args) { Shape[] s = new Shape[9]; for (int i = 0; i < s.length; i++)

s[i] = randShape(); //Make polymorphism method calls: for (int i = 0; i < s.length; i++) s[i].draw();}}

Upcasting and Polymorphism