0% found this document useful (0 votes)
57 views21 pages

Csc186topic 3 - Part1

This document discusses the concept of classes in object-oriented programming. It defines what a class is and explains that classes are templates that describe common objects. It provides examples of predefined and user-defined classes like the Bicycle class. It also covers key concepts like object declaration and creation, class definition including fields, constructors and methods, and access modifiers. Finally, it provides exercises for students to design a class diagram and define a Student class.

Uploaded by

Nurain Damia
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)
57 views21 pages

Csc186topic 3 - Part1

This document discusses the concept of classes in object-oriented programming. It defines what a class is and explains that classes are templates that describe common objects. It provides examples of predefined and user-defined classes like the Bicycle class. It also covers key concepts like object declaration and creation, class definition including fields, constructors and methods, and access modifiers. Finally, it provides exercises for students to design a class diagram and define a Student class.

Uploaded by

Nurain Damia
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/ 21

TOPIC 3: CONCEPT OF CLASSES 1

CSC186 OBJECT ORIENTED PROGRAMMING


LESSON OUTCOMES
Upon completion of this chapter, students should be able to:

▪ Describe the meaning of Class


▪ Describe the difference between object declaration and
creation
▪ Explain the component of class
▪ Understand on access modifier
▪ Write simple OOP program (using default constructor)

CSC186 OBJECT ORIENTED PROGRAMMING


INTRODUCTION TO CLASS
WHY USER/PROGRAMMER DEFINED CLASSES?

▪ Using predefined classes like the Scanner, String, and other


standard classes will not meet all of our needs.
▪ We need to be able to define our own classes customized for
our applications. Learning how to define our own classes is the
first step toward mastering the skills necessary in building large
programs.
▪ Classes we define ourselves are called user/programmer
defined classes.

3
INTRODUCTION TO CLASS
CLASS CONCEPT
A Java program is composed of one or more classes.
▪ A class is a template that describes the characteristics of a group
of objects of the same type.
▪ It is a description of a group of common objects.
▪ Some are predefined classes, others can be user/programmer
defined class.
▪ Example: Class - Bicycle

4
INTRODUCTION TO CLASS
WHY WE NEED A CLASS

bike1 bike2
ownerName - Adam Smith ownerName - Ben Jones

Class Diagram(UML Notation) 5


OBJECT DECLARATION

Class Name
This class must be defined Object Name
before this declaration can Object is declared here.
be stated.

Bicycle bike1,bike2;

CSC186 OBJECT ORIENTED PROGRAMMING 6


OBJECT CREATION

Object Name Class Name


Argument
Name of the object we are An instance of this
No arguments are used here.
creating here. class is created.

bike1 = new Bicycle( );


bike2 = new Bicycle( );

CSC186 OBJECT ORIENTED PROGRAMMING 7


DECLARATION VS. CREATION

1 Bicycle bike1;
2 bike1 = new Bicycle( );

bike1 1.The identifier bike1


1 declared and space is
allocated in memory.

2. A bike1 object is created


2 : Bicycle and the bike1 object is set
to refer to Bicycle class

CSC186 OBJECT ORIENTED PROGRAMMING 8


SENDING A MESSAGE

Object Name
Method Name Argument
Name of the object to
The name of the message The argument we are
which we are sending a
we are sending. passing with the message.
message.

bike1.setName(“Adam Smith”);

CSC186 OBJECT ORIENTED PROGRAMMING 9


INTRODUCTION TO CLASS
CLASS DEFINITION

A CLASS DEFINITION contains the implementation of


▪ Fields (data members)
▪ Constructors
– default
– normal
▪ Methods
– accessor (retriever)
– mutator (setter)
– toString
– processor
10
INTRODUCTION TO CLASS
CLASS DEFINITION
TEMPLATE FOR CLASS DEFINITION

Import Statements

Class Comment

class { Class Name

Data Members

Methods
(incl. Constructor)

}
11
INTRODUCTION TO CLASS
THE DEFINITION OF THE BICYCLE CLASS
class Bicycle {

// Data Member
private String ownerName;

public void Bicycle( ) {


ownerName = "Unknown";
}

public String getOwnerName( ) {

return ownerName;
}

public void setOwnerName(String name) {

ownerName = name;
}
} 12
INTRODUCTION TO CLASS
MULTIPLE INSTANCES

Once the Bicycle class is defined, we can create multiple


instances or an Object

bike1 bike2

Bicycle bike1, bike2;


: Bicycle : Bicycle
bike1 = new Bicycle( );
ownerName ownerName
bike1.setOwnerName("Adam Smith");
“Adam Smith” “Ben Jones”

bike2 = new Bicycle( );


bike2.setOwnerName("Ben Jones");

CSC186 OBJECT ORIENTED PROGRAMMING 13


INTRODUCTION TO CLASS
USING THE BICYCLE CLASS

public class BicycleApp


{ public static void main(String args[])
{ Bicycle bike1=new Bicycle();
Bicycle bike2=new Bicycle();

bike1.setOwnerName("Adam Smith");
bike2.setOwnerName("Ben Jones");

String owner1;
owner1=bike1.getOwnerName();

System.out.println("First owner:"+owner1);
System.out.println("Second
owner:"+bike2.getOwnerName());
}
} 14
INTRODUCTION TO CLASS
MEMBER ACCESS MODIFIER

▪ Java programming language supports four access modifiers for


member variables and methods : private, protected, public and if left
unspecified, it will invoke a package .
▪ Begin the declaration of each method or data member with one of
the access modifiers:

Access Modifier Definition

public Can be accessed from anywhere. the class,


data, or method is visible to any class in any
package

private
Can only be accessed from within the class
they are declared in.
15
INTRODUCTION TO CLASS
MEMBER ACCESS MODIFIER (cont’)

Access Modifier Definition

protected
Can only be accessed from within the class
they are declared in and sub-classes. (used
with inheritance)

package / default
Can be accessed within the class and
package/folder

Meaning of package : Conceptually you can


think of packages as being similar to different
folders on your computer

16
INTRODUCTION TO CLASS
MEMBER ACCESS MODIFIER (cont’)
Modifier Class Subclass Package World

private X

protected X X X

public X X X X

package/ X X
default

17
INTRODUCTION TO CLASS
DATA MEMBER DECLARATION

<modifiers> <data type> <name> ;

Modifiers Data Type Name

private String ownerName ;

Note: There’s only one modifier in this


example.

18
INTRODUCTION TO CLASS
MORE EXAMPLE(Let’s do it together)

1. Given the class diagram:

2. Write a class definition for Employee.


3. Write a main application that will define and
create 2 employee. It will input the data for this 2
employee and display the details of the older
employee.
19
INTRODUCTION TO CLASS
EXERCISE

Student class have 3 attributes which is name, matric number


and gpa. This class have default constructor, setter method and
get method. Your task:
a) Draw a class diagram for Student class.
b) Write the definition for Student class
c) Write an application that will :
i) Input a detail for 2 students
ii) Display the name and matric number of the
students that get higher gpa
iii)Display the average gpa
20
THANK YOU

CSC186 OBJECT ORIENTED PROGRAMMING

You might also like