Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Jan. 20041 Object Oriented Programming Yangjun Chen Dept. Business Computing University of Winnipeg."— Presentation transcript:

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

2 Jan. 20042 Outline: OOP What is OOP? Class, instance, field, method,... “this” key word Method Overloading Inheritance Method Overriding Abstract and Interface

3 Jan. 20043 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.

4 Jan. 20044 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.

5 Jan. 20045 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.

6 Jan. 20046 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( );

7 Jan. 20047 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.”);

8 Jan. 20048 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 }

9 Jan. 20049 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.

10 Jan. 200410 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

11 Jan. 200411 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.

12 Jan. 200412 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

13 Jan. 200413 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.

14 Jan. 200414 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.

15 Jan. 200415 “this” Keyword y = this.x; //the x instance variable for the current obejct this.myMethod(this);//call myMethod( ) //defined in this //class passing it the current object return this;//return the current object In many cases, it is possible to omit the “this” keyword entirely.

16 Jan. 200416 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”.

17 Jan. 200417 Implied “this”? class Car { String licensePlate; double speed, maxSpeed; void accToMax( ){ speed = maxSpeed; //leave out “this” } //accToMax } //class Car

18 Jan. 200418 “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’.

19 Jan. 200419 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

20 Jan. 200420 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.

21 Jan. 200421 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

22 Jan. 200422 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

23 Jan. 200423 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.

24 Jan. 200424 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.

25 Jan. 200425 “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.

26 Jan. 200426 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; }

27 Jan. 200427 Inheritance public 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 ( ) {…} }

28 Jan. 200428 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.

29 Jan. 200429 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.

30 Jan. 200430 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 }

31 Jan. 200431 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

32 Jan. 200432 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;

33 Jan. 200433 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( ).

34 Jan. 200434 Abstract public class Car extends MotorVehicle { …... public abstract void gas( ) { … }; … } MotorVehicle Car

35 Jan. 200435 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( ); }

36 Jan. 200436 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.

37 Jan. 200437 Interface import java.util.*; 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 ( ) { } public void swim ( ) { } public void fly ( ) { } }

38 Jan. 200438 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 CanFight u(h); //Treat it as a CanSwim v(h); //Treat it as a CanFly w(h); //Treat it as an ActionCharacter }

39 Jan. 200439 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.

40 Jan. 200440 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!”);} }

41 Jan. 200441 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 CanFight u(h); //Treat it as a CanSwim v(h); //Treat it as a CanFly w(h); //Treat it as an ActionCharacter }

42 Jan. 200442 HeroActionCharacterCanFightCanSwimCanFly subclassimplementation h instantiation Hero h = new Hero( ) t(h); //Treat it as a CanFight u(h); //Treat it as a CanSwim v(h); //Treat it as a CanFly w(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();}

43 Jan. 200443 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); }

44 Jan. 200444 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

45 Jan. 200445 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 }

46 Jan. 200446 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() Square draw() erase() Triangle draw() erase() Circle draw() erase()

47 Jan. 200447 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

48 Jan. 200448 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


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

Similar presentations


Ads by Google