09 Aslide OOP

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Chapter 9 Objects and Classes

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
1
Introduction to Object Oriented
Programming
 Object oriented programming (OOP) is THE
programming methodology for designing complex
systems
 OOP essentially is about the use of re-useable
software components called objects
 These objects can be prebuilt by third party vendors
to ‘plug in’ to our application or can be developed
by ourselves
– Perhaps building on existing objects

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Introduction to Object Oriented
Programming
 There are good reasons why building our application out
of such objects has major advantages
– We can abstract out the complex state and behaviour of
individual objects
– Only the interface between the objects in our
application is important
– We can use prebuilt objects such as Framework Class
Library (FCL) objects
 We are able to extend these objects to meet our own

needs

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
3
OO Programming Concepts
Object-oriented programming (OOP) involves
programming using objects. An object represents
an entity in the real world that can be distinctly
identified. For example, a student, a desk, a circle,
a button, and even a loan can all be viewed as
objects. An object has a unique identity, state, and
behaviors. The state of an object consists of a set
of data fields (also known as properties) with their
current values. The behavior of an object is defined
by a set of methods.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved. 4
Declaring/Creating Objects
in a Single Step
ClassName objectRefVar = new ClassName();

Assign object reference Create an object


Example:
Circle myCircle = new Circle();

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
Declaring/Creating Objects
 The process of using a type to create an object is
called instantiation
 In C#, C++, and Java the new keyword is used

Car myCar = new Car(“Peugeot 207”);

 We could create many Car objects but there is


Class Object
only one Car class

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
Objects
Class Name: Circle A class template

Data Fields:
radius is _______

Methods:
getArea

Circle Object 1 Circle Object 2 Circle Object 3 Three objects of


the Circle class
Data Fields: Data Fields: Data Fields:
radius is 10 radius is 25 radius is 125

An object has both a state and behavior. The state


defines the object, and the behavior defines what
the object does.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7
Classes
Classes are constructs that define objects of the
same type. A Java class uses variables to define
data fields and methods to define behaviors.
Additionally, a class provides a special type of
methods, known as constructors, which are
invoked to construct objects from the class.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Classes
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field

/** Construct a circle object */


Circle() {
}
Constructors
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}

/** Return the area of this circle */


double getArea() { Method
return radius * radius * 3.14159;
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
UML Class Diagram
UML Class Diagram Circle Class name

radius: double Data fields

Circle() Constructors and


Circle(newRadius: double) methods
getArea(): double
getPerimeter(): double
setRadius(newRadius:
double): void

circle2: Circle circle3: Circle UML notation


circle1: Circle
for objects
radius = 1.0 radius = 25 radius = 125

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
SimpleCircle.java

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
11
TestSimpleCircle.java

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
12
TestSimpleCircle.java
 Output:

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
13
What is a Constructor ?
 Use to Initialize data member when class
object is instantiated
 Two types :
– Constructor without parameter – default
constructor
– Constructor with parameter
 Overloading constructor

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
14
Constructors
Constructors are a special
Circle() { kind of methods that are
} invoked to construct objects.

Circle(double newRadius) {
radius = newRadius;
}

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
15
Constructors, cont.
A constructor with no parameters is referred to as
a no-arg constructor.
·       Constructors must have the same name as the
class itself.
·       Constructors do not have a return type—not
even void.
·       Constructors are invoked using the new
operator when an object is created. Constructors
play the role of initializing objects.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
16
Creating Objects Using
Constructors
new ClassName();

Example:
new Circle();

new Circle(5.0);

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
17
Default Constructor
A class may be defined without constructors. In
this case, a no-arg constructor with an empty body
is implicitly defined in the class. This constructor,
called a default constructor, is provided
automatically only if no constructors are explicitly
defined in the class.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
18
Example1: Declaring Constructor
public class UNIKL
{ Constructor name
       private String name;
       public UNIKL (String campus) Receiving parameter
{
               name = campus; Store value of campus into name
       }
}      
public class UNIKLTest
{
       public static void main(String args[ ])
{
               UNIKL univ = new UNIKL(“miit");
        }
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19
Example2: Declaring Constructor
public class Transportation
{
private String name;
Constructor name
private int year;
private String model;
No parameter
public Transportation( )
{
name=“Honda"; Store default value into data member
year=2021;
model=“Estima";
}

public void displayInfo( )


{
System.out.println("The default car name is "+name);
System.out.println("The default car year is "+year);
System.out.println("The default car model is "+model);
}
} Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
20
rights reserved.
Example2: Declaring Constructor
public class TransportationTest
{

public static void main(String[ ] args)


{ Create an instance called car

Transportation car = new Transportation();


car.displayInfo( ); Call displayInfo method

}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21
Example2: Declaring Constructor

 Output:

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
22
Example3: Declaring Constructor
public class Transportation
{
String name;
int year;
String model;

public Transportation (String CarName, int CarYear, String CarModel)


{
name=CarName;
year=CarYear;
model=CarModel

public void DisplayInfo( )


{
System.out.println("The default car name is "+name);
System.out.println("The default car year is "+year);
System.out.println("The default car model is "+model);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
23
Example3: Declaring Constructor
public class TransportationTest
{

public static void main(String[ ] args)


{

Transportation car = new ransportation(“Honda”,2021,”Estima”);


car.DisplayInfo( );

}
}

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
24
Example2: Declaring Constructor

 Output:

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
25
Declaring Object Reference Variables
To reference an object, assign the object to a reference
variable.

To declare a reference variable, use the syntax:

ClassName objectRefVar;

Example:
Circle myCircle;

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
26
Exercise
 Class Author has 3 data members as String id,
String authorName and int yearPublished.
– Create a constructor without parameter (default
constructor) that will assigned a default value of
“123”,”Abu Bakar” and 2021 when an instance of
author1 is created.
– Create a constructor with parameter that will pass
default value of “123”,”Abu Bakar” and 2021 when an
instance of author1 is created through parameter.
Create the test class too !. Compile and execute 

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
27
Exercise: Defining Classes and Creating Objects
TV
channel: int The current channel (1 to 120) of this TV.
volumeLevel: int The current volume level (1 to 7) of this TV.
on: boolean Indicates whether this TV is on/off.

The + sign indicates +TV() Constructs a default TV object.


a public modifier.
+turnOn(): void Turns on this TV.
+turnOff(): void Turns off this TV.
+setChannel(newChannel: int): void Sets a new channel for this TV.
+setVolume(newVolumeLevel: int): void Sets a new volume level for this TV.
+channelUp(): void Increases the channel number by 1.
+channelDown(): void Decreases the channel number by 1.
+volumeUp(): void Increases the volume level by 1.
+volumeDown(): void Decreases the volume level by 1.

Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
28

You might also like