0% found this document useful (0 votes)
14 views7 pages

Lecture 8

The document introduces object-oriented programming concepts including classes, objects, and UML class diagrams. A class defines objects through data fields and methods. Constructors construct objects from classes. The document provides examples of a Circle class and objects to illustrate these concepts.

Uploaded by

Shamim Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Lecture 8

The document introduces object-oriented programming concepts including classes, objects, and UML class diagrams. A class defines objects through data fields and methods. Constructors construct objects from classes. The document provides examples of a Circle class and objects to illustrate these concepts.

Uploaded by

Shamim Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Motivations

• After learning the preceding chapters, you are capable


of solving many programming problems using
selections, loops, methods, and arrays.
Chapter 9 • However, these Java features are not sufficient for
Objects and Classes developing graphical user interfaces and large scale
software systems.
• Suppose you want to develop a graphical user interface
as shown below. How do you program it?

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 1 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 2
rights reserved. 0132130807 rights reserved. 0132130807

OO Programming Concepts Objects


• Object-oriented programming (OOP) involves Class Name: Circle A class template

programming using objects. Data Fields:


radius is _______

• An object represents an entity in the real world that can be Methods:


getArea

distinctly identified.
• For example, a student, a desk, a circle, a button, and even Circle Object 1 Circle Object 2 Circle Object 3 Three objects of
the Circle class
Data Fields: Data Fields: Data Fields:
a loan can all be viewed as objects. radius is 10 radius is 25 radius is 125

• An object has a unique identity, state, and behaviors.


An object has both a state and behavior. The state
• The state of an object consists of a set of data fields (also
defines the object, and the behavior defines what
known as properties) with their current values.
the object does.
• The behavior of an object is defined by a set of methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 4
rights reserved. 0132130807 rights reserved. 0132130807
Classes Classes
class Circle {
/** The radius of this circle */
• Classes are constructs that define objects of the double radius = 1.0; Data field
same type.
/** Construct a circle object */
Circle() {
• A Java class uses variables to define data fields }
Constructors
and methods to define behaviors. /** Construct a circle object */
Circle(double newRadius) {
• Additionally, a class provides a special type of radius = newRadius;
}
methods, known as constructors, which are
/** Return the area of this circle */
invoked to construct objects from the class. double getArea() { Method
return radius * radius * 3.14159;
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 6
rights reserved. 0132130807 rights reserved. 0132130807

UML Class Diagram Constructors


Constructors are a special
UML Class Diagram Circle Class name Circle() { kind of methods that are
radius: double Data fields } invoked to construct objects.
Circle() Constructors and
Circle(newRadius: double) methods
getArea(): double
Circle(double newRadius) {
circle1: Circle circle2: Circle circle3: Circle UML notation
for objects
radius = newRadius;
radius = 1.0 radius = 25 radius = 125
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 7 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 8
rights reserved. 0132130807 rights reserved. 0132130807
Constructors, cont. Creating Objects Using Constructors
• A constructor with no parameters is referred to
as a no-arg constructor. new ClassName();

• Constructors must have the same name as the


class itself. Example:
new Circle();
• Constructors do not have a return type—not
even void.
new Circle(5.0);
• Constructors are invoked using the new
operator when an object is created. Constructors
play the role of initializing objects.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 9 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 10
rights reserved. 0132130807 rights reserved. 0132130807

Default Constructor Declaring Object Reference Variables


• A class may be declared without constructors. • To reference an object, assign the object to a
reference variable.
• In this case, a no-arg constructor with an empty
body is implicitly declared in the class. • To declare a reference variable, use the syntax:
• This constructor, called a default constructor, is
provided automatically only if no constructors ClassName objectRefVar;
are explicitly declared in the class.
Example:
Circle myCircle;

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 11 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 12
rights reserved. 0132130807 rights reserved. 0132130807
Declaring/Creating Objects Accessing Objects
in a Single Step • Referencing the object’s data:
objectRefVar.data
ClassName objectRefVar = new ClassName();
e.g., myCircle.radius
Assign object reference Create an object
Example: • Invoking the object’s method:
Circle myCircle = new Circle();
objectRefVar.methodName(arguments)
e.g., myCircle.getArea()

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 13 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 14
rights reserved. 0132130807 rights reserved. 0132130807

Trace Code Trace Code, cont.


Declare myCircle

Circle myCircle = new Circle(5.0); no value Circle myCircle = new Circle(5.0); no value
myCircle myCircle
SCircle yourCircle = new Circle(); Circle yourCircle = new Circle();

yourCircle.radius = 100; yourCircle.radius = 100; : Circle

radius: 5.0

Create a circle

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 15 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 16
rights reserved. 0132130807 rights reserved. 0132130807
Trace Code, cont. Trace Code, cont.
Circle myCircle = new Circle(5.0); reference value
myCircle
Circle yourCircle = new Circle();
Circle myCircle = new Circle(5.0); myCircle reference value
yourCircle.radius = 100; : Circle
Circle yourCircle = new Circle();
radius: 5.0
yourCircle.radius = 100; Assign object reference : Circle
to myCircle
radius: 5.0 yourCircle no value

Declare yourCircle

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 17 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 18
rights reserved. 0132130807 rights reserved. 0132130807

Trace Code, cont. Trace Code, cont.


Circle myCircle = new Circle(5.0); reference value Circle myCircle = new Circle(5.0); reference value
myCircle myCircle
Circle yourCircle = new Circle(); Circle yourCircle = new Circle();

yourCircle.radius = 100; : Circle yourCircle.radius = 100; : Circle

radius: 5.0 radius: 5.0

yourCircle no value yourCircle reference value

Assign object reference


: Circle to yourCircle : Circle
Create a new radius: 0.0 radius: 1.0
Circle object

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 19 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 20
rights reserved. 0132130807 rights reserved. 0132130807
Trace Code, cont. Caution
Circle myCircle = new Circle(5.0);
myCircle reference value Recall that you use
Circle yourCircle = new Circle(); Math.methodName(arguments) (e.g., Math.pow(3, 2.5))

yourCircle.radius = 100; : Circle


to invoke a method in the Math class. Can you invoke getArea() using
radius: 5.0 Circle1.getArea()? The answer is no. All the methods used before this
chapter are static methods, which are defined using the static keyword.
However, getArea() is non-static. It must be invoked from an object
yourCircle reference value
using

: Circle objectRefVar.methodName(arguments) (e.g., myCircle.getArea()).


Change radius in radius: 100.0
yourCircle More explanations will be given in the section on “Static Variables,
Constants, and Methods.”

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 21 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 22
rights reserved. 0132130807 rights reserved. 0132130807

Reference Data Fields The null Value


The data fields can be of reference types. For example, If a data field of a reference type does not
the following Student class contains a data field name of
the String type. reference any object, the data field holds a
special literal value, null.
public class Student {
String name; // name has default value null
int age; // age has default value 0
boolean isScienceMajor; // isScienceMajor has default value false
char gender; // c has default value '\u0000'
}

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 23 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 24
rights reserved. 0132130807 rights reserved. 0132130807
Default Value for a Data Field Example
• The default value of a data field is
– null for a reference type, Java assigns no default value to a local variable
– 0 for a numeric type, inside a method.
– false for a boolean type, and
– '\u0000' for a char type. public class Test {
• However, Java assigns no default value to a local public static void main(String[] args) {
variable inside a method. int x; // x has no default value
String y; // y has no default value
public class Test { System.out.println("x is " + x);
public static void main(String[] args) { System.out.println("y is " + y);
Student student = new Student(); }
System.out.println("name? " + student.name); }
System.out.println("age? " + student.age);
System.out.println("isScienceMajor? " + student.isScienceMajor);
System.out.println("gender? " + student.gender);
} Compilation error: variables not
} initialized
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 25 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 26
rights reserved. 0132130807 rights reserved. 0132130807

Differences between Variables of


Primitive Data Types and Object Types

Created using new Circle()


Primitive type int i = 1 i 1

Object type Circle c c reference c: Circle

radius = 1

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All 27
rights reserved. 0132130807

You might also like