0% found this document useful (0 votes)
9 views46 pages

Lect 6

This lecture introduces the concepts of classes and objects in object-oriented programming, explaining that a class serves as a blueprint for creating objects with properties and behaviors. It covers the structure of a class, including variables, constructors, and methods, and demonstrates how to instantiate objects and access their properties using the dot operator. Additionally, it discusses the use of the 'this' keyword, the toString() method, and the concept of constructor overloading.

Uploaded by

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

Lect 6

This lecture introduces the concepts of classes and objects in object-oriented programming, explaining that a class serves as a blueprint for creating objects with properties and behaviors. It covers the structure of a class, including variables, constructors, and methods, and demonstrates how to instantiate objects and access their properties using the dot operator. Additionally, it discusses the use of the 'this' keyword, the toString() method, and the concept of constructor overloading.

Uploaded by

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

CSEN 202

Introduction to Computer Programming

Lecture 6
Classes and Object (part 1)

Dr. Milad Ghantous Spring 2025 GUC


Classes and objects
• Sometimes, we need more than 1 property (value) to represent an
object.
• Unlike a primitive integer variable for example, which only has a
value, a car, for example, might have multiple properties from
different types to represent it such as make, model, year, engine
power, color, and so on…
• Strings, as we said in previous lectures, is actually not a primitive
type, but a complex (object) type since it has a value and methods
such as length() and chatAt(),…
Spring 2025 Dr. Milad Ghantous 2
Classes versus objects
• In object-oriented programming, a class is a blueprint or a
template that defines the properties and behaviors of an object.

• It is a user-defined data type that encapsulates data and functions


that operate on that data.

• A class is like a blueprint for creating multiple instances of objects


that share the same properties and behaviors.

Spring 2025 Dr. Milad Ghantous 3


Classes versus objects
• An object, on the other hand, is an instance of a class. It is
created from a class and represents a specific instance of that
class, with its own unique data and behavior.

• An object is an entity that contains data and the methods that


operate on that data.

• Objects are created dynamically during program execution and


can interact with each other.

Spring 2025 Dr. Milad Ghantous 4


Summary Class Class

Classes provide
the structure
(mold) for
creating objects,
while objects are
the actual entities
that contain data
and behavior.

Objects Objects

Spring 2025 Dr. Milad Ghantous 5


A class structure public class myClass {

• Classes have 3 main parts: Variables


1. Variable(s)
2. Constructor(s)
Constructors
3. Method(s)

Methods

Spring 2025 Dr. Milad Ghantous 6


Parts of a class
• Variables are used to describe your objects. For a car object,
variables can be be make, model, year. For a Student object,
variables can be name, ID, age, GPA,…

• A Constructor has the same name as the class, looks like a


method, without a return type and is used whenever a new
object is initialized/created.

• Methods are used to either read the variable values of the


object or to change them.
Spring 2025 Dr. Milad Ghantous 7
Example 1: A class Car
• Let’s create a class (a mold) for a generic car.

• Later on, we will instantiate objects of this class (create


instances)

• Note that when you create a class, you are creating a new type
in java that you can use to create instances of this type.

Spring 2025 Dr. Milad Ghantous 8


public class Car {

Car String make;


make
model
variables
String model; year
int year; Car(ma,mo,y)

public Car(String ma, String mo, int y)


{
make = ma;
model = mo; Constructor
year = y;

}
}

Spring 2025 Dr. Milad Ghantous 9


But we created only the mold, how to
create objects using this mold?
• Inside the main method, we can use our constructor to create
as many objects as we want.

• Syntax: Car c = new Car(make, model, year)

• Look next.

Spring 2025 Dr. Milad Ghantous 10


public class Car {
String make;
Car String model;
int year;

public Car(String ma, String mo, int y)


{ make = ma;
model = mo;
year = y;
}

public static void main(String[] args) {

Car c1 = new Car("BMW", "3-series", 2020);


Car c2 = new Car("Tesla", "model S", 2022);
}

Spring 2025 Dr. Milad Ghantous 11


You can think about it this way

Enter name: c1
ma bmw
mo 3-series
y 2020

Spring 2025 Dr. Milad Ghantous 12


Car c1 = new Car("BMW", "3-series", 2020);
Car c2 = new Car("Tesla", "model S", 2022);
Car
In reality, they are represented like this in java

BMW Tesla
3-series Model S
2020 2022

C1 C2

Spring 2025 Dr. Milad Ghantous 13


The dot operator
• To access variables (and methods) of the object, the dot
operator is used.
Output
Car c1 = new Car("BMW", "3-series", 2020);
Car c2 = new Car("Tesla", "model S", 2022);
System.out.println(c1.make);
System.out.println(c2.model);

Spring 2025 Dr. Milad Ghantous 14


The dot operator
• We can also edit variables using the dot operator

Output
System.out.println(c1.model);
c1.model = "X6";
System.out.println(c1.model);

Spring 2025 Dr. Milad Ghantous 15


Printing an object
Car c1 = new Car("BMW", "3-series", 2020);
Car c2 = new Car("Tesla", "model S", 2022);
System.out.println(c1);
System.out.println(c2);
Output

Unlike what we expected, when you try to print an


object, we get its reference, which is the location in
memory and not the values of its variables.

Spring 2025 Dr. Milad Ghantous 16


Memory (RAM)
reference Content
Object reference …
BMW
5a07e868 c1 3-series
2020

Car c1 = new Car("BMW", "3-series", 2020); Tesla


v
76ed5528
c2 Model S
Car c2 = new Car("Tesla", "model S", 2022); 2022

v

v

Spring 2025 Dr. Milad Ghantous 17


So how to print the object’s info?
• You can just print them using the dot operator, one by one.

• Or you can create a static method that receives the object as a


parameter, and then prints its content using the dot operatos.

Spring 2025 Dr. Milad Ghantous 18


public class Car {

// variables and constructors not shown just for limited slide space.

public static void displayInfo(Car c)


{
System.out.println("Make:"+c.make+", Model:"+c.model+", year:"+c.year);
}

public static void main(String[] args) {

Car c1 = new Car("BMW", "3-series", 2020);


Car c2 = new Car("Tesla", "model S", 2022);
displayInfo(c1);
displayInfo(c2);
} Output
}

Spring 2025 Dr. Milad Ghantous 19


Can we do this?

Output

Spring 2025 Dr. Milad Ghantous 20


Part 2
String objects and instance methods

Spring 2025 Dr. Milad Ghantous 21


Strings
• In previous lectures, we mentioned that
strings are objects and not a primitive
type.
Output
• So why don’t we use a constructor with
the new keyword when we create them?

• Answer: we can!  but Java wanted our


lives to be easier since String is a built-
in class and not a user defined one.

Spring 2025 Dr. Milad Ghantous 22


String methods
• When we used methods with strings, we used to use the dot
operator to access them.
• Examples: s.chatAt(0), s.length() and so on.
• We didn’t have to send the string as a parameter.
• These are called instance methods and we can create them
for our Car class as well.
• Instance methods do NOT have the static keyword in their
signature.

Spring 2025 Dr. Milad Ghantous 23


Creating instance methods for Car
Instance methods
have access to the
class variables so
we can use them
right away. No
public class Car {
need to pass an
// variables and constructors
object to them.
// not shown just for limited slide space.
Look next.
public void displayInfo()
instance

{
System.out.println("Make:” + make +", Model:” + model +", year:” + year);

public static void displayInfo(Car c){


Static

System.out.println("Make:"+c.make+", Model:"+c.model+", year:"+c.year);

Spring 2025 Dr. Milad Ghantous 24


So how to call instance methods?
public static void main(String[] args) {

Car c1 = new Car("BMW", "3-series", 2020);


Car c2 = new Car("Tesla", "model S", 2022); Output
c1.displayInfo();
c2.displayInfo();
}

Output
Can we do this?
Yes! Java understands
which is which.

Spring 2025 Dr. Milad Ghantous 25


A visual representation of static versus instance methods
Static Instance

make Obj displayInfo(Obj)


model
year
Method make Instance method
belongs to the
model class instance
and can access
• The object is passed as an argument. Method will year its variables right
use dot operator to access its variables and use away. No need to
them. pass anything to
• Also the method is not necessarily created in the it.
same class. It could be created in another class
• Static methods are INDEPENDENT OF CLASS
displayInfo()
INSTANCE!

Spring 2025 Dr. Milad Ghantous 26


Part 3
This keyword

Spring 2025 Dr. Milad Ghantous 27


This keyword
• You can refer to the class variables by adding this. before them.
• This helps you organize your code, be more readable, and helps
java as well in confusing situations. this refers the class where it’s
written.
• Re-writing our constructor:
public Car(String ma, String mo, int y)
{ this.make = ma;
this.model = mo;
this.year = y;
}
Spring 2025 Dr. Milad Ghantous 28
Now, we can do this!
public Car(String make, String model, int year)
{
this.make = make;
this.model = model;
this.year = year;
}

Java knows exactly now that this.make is the class make variable, while make
(without this) is the parameter in the arguments list of the constructor.

Spring 2025 Dr. Milad Ghantous 29


What happens when we do this?
public Car(String make, String model, int year)
{
make = make;
model = model;
year = year;
}

All of them are seen as the parameters of the method, and hence our class
variables are left untouched. This means, that java will assign default
values for them, depending on their type. Look next.
Spring 2025 Dr. Milad Ghantous 30
Watch what happens
public Car(String make, String model, int year)
{
make = make;
model = model;
year = year;

public static void main(String[] args) {

Car c1 = new Car("BMW", "3-series", 2020);


Output
c1.displayInfo();

Spring 2025 Dr. Milad Ghantous 31


Part 4
toString() method

Spring 2025 Dr. Milad Ghantous 32


toString() method
• The toString() method is built-in in any object, and when you try
to print an object using system.out.print, the toString() is called.
• But it prints the object reference as shown before.

These two statements are equivalent

Spring 2025 Dr. Milad Ghantous 33


We can override the toString() method
• The programmer can override the toString() method to whatever he/she wants.
• In our example, we will replace displayInfo by toString().

public String toString()


{
return "Make:"+make+", Model:"+model+", year:"+year;
}

Output

Spring 2025 Dr. Milad Ghantous 34


Part 5
Creating multiple constructors

Spring 2025 Dr. Milad Ghantous 35


Can we create more than 1 constructor?
• Of course! Using overloading.

• You can create multiple versions of the constructor, by either


omitting some arguments and defining a default value for them
for all newly created object, or keep all the arguments and
change their order.

• Java will know which constructor to call, depending on how you


call it.

Spring 2025 Dr. Milad Ghantous 36


Multiple 1

constructors
How to use them?

3
Output

Spring 2025 Dr. Milad Ghantous 37


Note
• If you don’t create any constructor, java uses a default
constructor which takes no parameters: Car()

• You can use it to create new Car objects, which of course, will
have default values.

• But once you create 1 version of a constructor, the default one


Car() cannot be used anymore.

Spring 2025 Dr. Milad Ghantous 38


Example 2: The Point class
• Let’s create a class that represents a cartesian point in 2-D
space.

• We need to define its x and y coordinates


• A constructor (or more)
• Any methods we need (instance or static)

Spring 2025 Dr. Milad Ghantous 39


But before we continue
• You can create several classes
(class files) in one folder: Car,
point and Tester.

• Only one class has to have one a


main method (you can have more).
For example: Tester class here.
Car.java Point.java Tester.java

• All classes can “See” other classes


in the same folder and use them.
Spring 2025 Dr. Milad Ghantous 40
public class Point
{
Point double x;
double y;
Point.java
public Point(double x, double y)
{
this.x = x;
this.y = y;

}
public String toString(){
return "(" + x + "," + y + ")" ;
}

}
Spring 2025 Dr. Milad Ghantous 41
Tester

public class Tester { Point.java Tester.java

public static void main(String[] args) {

Point p1 = new Point(5,6);


Point p2 = new Point(1.6,2.5);

System.out.println(p1.toString());
Output
System.out.println(p2.toString());
}}

Spring 2025 Dr. Milad Ghantous 42


Task 1
Create a method that translates a point by dx and dy
• Static or instance?
• We can do either!

• Static one: we create it in the tester class and accepts


an object parameter of type point

• Instance one: we create it in the point class and it


doesn’t need any parameter.
Spring 2025 Dr. Milad Ghantous 43
Static way: create it in the Tester class
public class Tester {

public static void translate(Point p, double dx, double dy)


{
p.x += dx;
p.y += dy;
}

public static void main(String[] args) {


Point p1 = new Point(5,6);
Point p2= new Point(1.6,2.5); Output
translate(p1, 0.5, -3);
System.out.println(p1.toString()); (5.5 , 3.0)
}}
Spring 2025 Dr. Milad Ghantous 44
Instance one: only in lecture 
• Where would you create it?
• How would you use it?

Spring 2025 Dr. Milad Ghantous 45


End of lecture 6

Spring 2025 Dr. Milad Ghantous 46

You might also like