1 represent the current state of the object example ...courses/coe808/lecture82.pdf · • current...

29
1 Java Tutorial What is an Object? 1 Variables represent the current state of the object Example: Bicycle: Current Speed is 10 mph Current Pedal cadence is 90 rpm (revolution per minutes) Current gear is 5th 2 Methods inspect or change the state of a particular object (bicycle). Brake method Change gears Change the pedal cadence

Upload: others

Post on 31-Mar-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

1

Java Tutorial What is an Object?

1 Variables represent the current state of the object Example: Bicycle: • Current Speed is 10 mph • Current Pedal cadence is 90 rpm (revolution per minutes) • Current gear is 5th

2 Methods inspect or change the state of a particular object (bicycle). • Brake method • Change gears • Change the pedal cadence

Page 2: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

2

What is a Class? A blueprint that defines the instance variables and the methods common to

all objects of a certain kind. • After you've created the class, you can create any number of objects from

the class. • When you create an object of a class, the system allocates enough memory

for the object. Constructors Creates a new object of the class. It initializes all the instance variables and does any work necessary to prepare the class to be used. Car c = new Car();

Page 3: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

3

class Car { String licensePlate; double speed; double maxSpeed; void SetlicensePlate(String licencePlate1 ) { licencePlate = licencePlate1; } double GetSpeed () { return speed; } void SetSpeed (int speed1) { speed =speed1; } }

class CarTest { public static void main(String args[]) { Car c = new Car(); c.SetlicensePlate(“Toronto66”); c.SetSpeed (150); System.out.println(c.licensePlate); System.out.println(c.GetSpeed()); }

Page 4: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

4

Access Protection

• Public • Private • Protected

violate encapsulation

Enforce encapsulation

Provide services to clients

Support other methods in the

class

public private

Variable

Method

Page 5: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

5

Inheritance Code reusability is claimed to be a key advantage of object-oriented languages.

Class Vehicle { public int a; private int b; protected int c; public method1() {…… } private method2() { ……} }

Class Car extends Vehicle { // variables //Methods }

Page 6: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

6

Method Declarations •A method declaration specifies the code that will be executed when the method is invoked (called) If the called method is in the same class, only the method name is needed

myMethod();

myMethod compute

Page 7: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

7

•The called method is often part of another class or object

doIt

helpMe

helpMe();

obj.doIt();

main

Page 8: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

8

Method Overloading

•Method overloading is the process of giving a single method name multiple definitions

float tryMe(int x) { return x + .375; }

float tryMe(int x, float y) { return x*y; }

result = tryMe(25, 4.32)

Invocation

Page 9: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

9

Arrays of Objects

•The elements of an array can be object references •The following declaration creates an array object called verbs and fills it with five String objects created using string literals String verbs[5] = {"play", "work", "eat", "sleep", “walk”};

“Play” verb

“Work”

“eat”

“sleep”

“walk”

Page 10: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

10

Overriding Methods A subclass inherits methods from a super class. Sometimes, it is necessary for the sub class to modify the methods defined in the super class.

public class Circle { protected double radius; protected double Area; public Circle(double radius) { this.radius = radius; }

public double getArea() { return (Area= Math.PI*radius*radius); } //this method return the area of the circle } // end of class circle

Cylinder GetArea()

Circle GetArea()

Page 11: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

11

public class Cylinder extends Circle { protected double length; public Cylinder(double radius, double length)

{ super(radius); this.length = length; }

public double getArea() // method overriden here { return ( 2*super.getArea()+2*Math.PI*radius*length); } //this method return the surface area of the cylinder } // end of class Cylinder

Page 12: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

12

Abstract Classes • Create a common interface for all the classes derived from it. • It can be expressed differently for each different subclass. • If you inherit from an abstract class, you must provide method definitions for all the

abstract methods. If you don’t, then the derived class is also abstract. • Abstract class express only the interface, not a particular implementation, so creating

an object of abstract class makes no sense,

Page 13: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

13

Interface – more abstract than abstract class • A collection of abstract methods and constants. • Defines a set of methods but does not implement them. A class that implements the

interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behavior.

• An interface is a function specification about what a class do, not how it does.

_

Page 14: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

14

“this” and “super” reference

Access to overriden methods and variables can be obtained with the super keyword:

public class A { int i=0; int j=3; void doSomething(){i=5;} }

class B extends A { int j=0; void doSomething(){ j=10; // Call the overridden method super.doSomething(); // And can use the shadowed // property j=j+super.j; } }

Page 15: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

15

We can also explicitly reference instance variables in the current object with the this keyword:

public class A{ int x,y; void doSomething(int x, int y){ // x & y hold the values passed in // the argument variables. To access // the instance variables x,y we must // specify them with 'this'. this.x = x; this.y = y; } }

If we have overloaded constructors, we can access other constructors from within one constructor using this():

public class A{ int x,y; int i,k; public A(int x, int y){ this.x = x; this.y = y; } public A(int x, int y, int i, int k){ this(x,y);// Must be in first line this.i = i; this.k = k; }}

Page 16: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

16

Similarly, we can access overridden constructors using super():

public class A { int i; public A(int i){this.i=i;} }

class B extends A { int j; public B(int i, int j){ super(i);// Must be in // first line this.j=j; } }

If there is no call to super class constructors in the first line of the sub-class, it will call the defualt constructor A() for the base class.

Page 17: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

17

Parameters Passing • When a method is called, the actual parameters in the invocation are copied into the formal parameters in the method header • Passing parameters is similar to an assignment statement Pass by Value: Public static void main(String [] args){ double d = 2.0;

changeMe(d); System.out.println(d);

} public void changeMe(double d) {

//this has no effect on d outside of this method! d = 345.0;

}

What’s the output of d?

Page 18: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

18

Pass by Reference:

• The actual parameter and the formal parameter become aliases of each other

Suppose class Car has methods called setSpeed() and getSpeed(). Car ferrari = new Car(); ferrari.setSpeed(100); changeParameters(ferrari); System.out.println(“The speed = “+ferrari.getSpeed()); public void changeParameters(Car c) { //changes the car’s speed outside this method! c.setSpeed(200.0); } What gets printed out?

Page 19: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

19

What is printed as a result of executing the following program? public class ArrayTest { public static void main(String[] args) {

int[] test = new int[2]; test[0] = test[1] = 5; System.out.println(test[0] + "," + test[1]); fiddle(test, test[1]); System.out.println(test[0] + "," + test[1]); }

static void fiddle(int[] test, int element) { test[0] = 10; test[1] = 11; element = 12; System.out.println(test[0] + "," + test[1] + "," + element); test = new int[2]; test[0] = 20; test[1] = 21; System.out.println(test[0] + "," + test[1]); }

}

Page 20: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

20

Page 21: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

21

Page 22: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

22

Page 23: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

23

Page 24: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

24

Page 25: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

25

Page 26: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

26

Page 27: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

27

Page 28: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

28

What is the output of a call to B.demo()? public class A { public void foo() {

System.out.println("A foo"); bar(); } public void bar() {

System.out.println("A bar"); }} public class B extends A { public void foo() {

System.out.println("B foo"); super.foo();

} public void bar() {

System.out.println("B bar"); } public static void demo() {

A a = new A(); B b = new B(); a.foo(); b.foo();}}

A foo() bar()

B foo() bar() demo()

Answer: A foo A bar B foo

A foo B bar

Page 29: 1 represent the current state of the object Example ...courses/coe808/lecture82.pdf · • Current gear is 5th 2 . Methods. inspect or change the state of a particular object (bicycle)

29

(a) Comment out any lines that cause a compilation error.

(b) Once the incorrect lines are eliminated, the class is compiled and run. What is the

output? 6