Object Oriented Programming - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Object Oriented Programming

Description:

The accToMax( ) method is completely enclosed within the Car class. ... In the body of a method definition, there could be times when you want to refer ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 49
Provided by: che88
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Programming


1
Object Oriented Programming Yangjun Chen Dept.
Business Computing University of Winnipeg
2
Outline OOP
  • What is OOP?
  • Class, instance, field, method, ...
  • this key word
  • Method Overloading
  • Inheritance
  • Method Overriding
  • Abstract and Interface

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.

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.

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.

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 classs
    constructor Car().
  • This can be done in a single line as follows
  • Car c new Car( )

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.)

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
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.

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

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.

12
Invoking Instance Methods
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
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.

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
    objects 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
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
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
Implied this?
class Car String licensePlate double
speed, maxSpeed void accToMax( ) speed
maxSpeed //leave out this
//accToMax //class Car
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.

19
Passing Arguments to Methods
  • Lets 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 gt this.maxSpeed)
  • this.speed this.maxSpeed
  • if (this.speed lt 0.0)
  • this.speed 0.0
  • //end of accelerate

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.

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 gt
this.maxSpeed) this.speed
this.maxSpeed if (this.speed lt 0.0)
this.speed 0.0 //end of
accelerate //class Car
22
Class CarTestYetAgain
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 lt 15
i) c.accelerate(10.0) System.out.printl
n(c.licensePlate is moving at c.speed
km/h.) //main //class
23
Returning Values from Methods
  • Using the return keyword at the end of a method
    and declaring the type to be returned in the
    methods signature.
  • Lets 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
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
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
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 doesnt want.
  • To inherit from a class, use the extends keyword
  • public class Compact extends Car
  • int x 0
  • int y 0

27
Inheritance
public class Compact int x 0 int y
0 String licensePlate double speed,
maxSpeed public Car(String licensePlate,
double speed, double maxSpeed) this.licensePa
lte licensePlate this.speed
speed this.maxSpeed maxSpeed public
Car(String licensePlate, double maxSpeed)
this(licensePlate, 0.0, maxSpeed) void
accTomax ( ) void accelerate ( )
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.

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.

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

31
Method Overriding
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 ( )
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

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( ).

34
Abstract
public class Car extends MotorVehicle
... public abstract void gas( )
MotorVehicle
Car
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( )

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.
  • Well use interface later on.

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 ActionCharacter implements
CanFight, CanSwim, CanFly public void fight (
) public void swim ( ) public void fly
( )
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 CanFight u(h) //Treat it as a
CanSwim v(h) //Treat it as a CanFly w(h)
//Treat it as an ActionCharacter
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.

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 implement
s 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
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
subclass
implementation
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()
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
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)
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
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) //Upcast
ing
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())
47
Upcasting and Polymorphism
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())
48
Upcasting and Polymorphism
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 Shape9 for
(int i 0 i lt s.length i) si
randShape() //Make polymorphism method
calls for (int i 0 i lt s.length i)
si.draw()
Write a Comment
User Comments (0)
About PowerShow.com