0% found this document useful (0 votes)
3 views31 pages

Chapter 4 - Objects and Classes

Chapter Four covers the fundamentals of objects and classes in Java, emphasizing the concepts of object variables, class definitions, and the instantiation and usage of objects. It explains the characteristics of objects, the role of constructors, and the difference between methods and constructors. Additionally, it provides examples of creating and accessing instance variables and methods within classes.
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)
3 views31 pages

Chapter 4 - Objects and Classes

Chapter Four covers the fundamentals of objects and classes in Java, emphasizing the concepts of object variables, class definitions, and the instantiation and usage of objects. It explains the characteristics of objects, the role of constructors, and the difference between methods and constructors. Additionally, it provides examples of creating and accessing instance variables and methods within classes.
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/ 31

Chapter Four

Objects and Classes

Here starts
the lesson!

Instructor – Zaheredin S.
Chapter Four Contents! Chapter Four Contents!

4.1 – Object variables 4.3 – Instantiating and using


objects
4.2 – Defining a class
4.4 – Instance fields,
Constructors and methods

Instructor – Zaheredin S.
4.1 – Object Variables
● We know that, java is an Object-Oriented programming
language.
● As a language that has the Object Oriented feature,
Java supports the following fundamental concepts:
 Polymorphism  Classes
 Inheritance  Objects
 Encapsulation  Method
 Abstraction  Message Parsing

1
Instructor – Zaheredin S.
Cont…
Object in java
● An entity that has state and behavior is known as an
object. It can be physical or logical (tangible and
intangible).
● The example of intangible object is banking system.
Let us now look deep into what are objects.
● If we consider the real world we can find many objects
around us, Cars, Dogs, Humans, etc. All these objects
have a state and behavior.

2
Instructor – Zaheredin S.
Cont…
● If we consider a dog, then its state is - name, breed,
color, and the behavior is - barking, wagging, running.
● Another Example: Pen is an object. Its name is Reynolds,
color is white etc. known as its state. It is used to
write, so writing is its behavior.
● If you compare the software object with a real world
object, they have very similar characteristics.
● Software objects also have a state and behavior.
● A software object's state is stored in fields and
behavior is shown via methods.

3
Instructor – Zaheredin S.
Cont…
● Therefore, in software development, methods operate on
the internal state of an object and the object-to-object
communication is done via methods.
● Object is an instance of a class. Class is a template or
blueprint from which objects are created. So object is
the instance(result) of a class.

4
Instructor – Zaheredin S.
Cont…
An object has three characteristics:
● state: represents data (value) of an object.
● behavior: represents the behavior (functionality) of an
object.
● identity: Object identity is typically implemented via a
unique ID. The value of the ID is not visible to the
external user. But, it is used internally by the JVM to
identify each object uniquely.

5
Instructor – Zaheredin S.
4.2 - Defining a class
Classes in Java
● A class is a group of objects that has common
properties and behaviour.
● A class is a blue print from which individual objects are
created or it describes the behaviors/states that object
of its type support.
● Syntax to declare a class:
class <class_name>{
data member;
method;
}
6
Instructor – Zaheredin S.
A sample of a class is given below
A class can contains
class Dog{
String breed; ● Any number of methods to
String color; access the value of various
int age; kinds of methods. In the
void barking(){ above example, barking(),
hungry() are methods.
} ● data member
void hungry(){ ● constructor
block
}

} ● class and interface

7
Instructor – Zaheredin S.
4.3 - Instantiating and using objects
Creating an Object
● In java, creating an object of the class is called
instantiation.
● As mentioned previously, a class provides the blueprints
for objects.
● Therefore, an object is created from a class.
● In Java, the new key word is used to create new
objects.
● Syntax for object instantiation.
ClassName objName = new ClassName();
8
Instructor – Zaheredin S.
Cont…
There are three steps when creating an object from a class:
● Declaration: A variable declaration with a variable name
with an object type.
● Instantiation: The 'new' key word is used to create the
object.
● Initialization: The 'new' keyword is followed by a call to
a constructor. This call initializes the new object.

Example of creating and using an object is given below:

9
Instructor – Zaheredin S.
Cont…
class Student{
int id;
String name;
public static void main(String []args){
// creating an object std
Student std = new Student();
System.out.println(std.id);
System.out.println(std.name);
Output
} 0
} null

10
Instructor – Zaheredin S.
Difference between instantiation and initialization
Instantiation initialization
● Creating an object by using ● Assigning a value to a
the new keyword is called variable is called
Instantiation. initialization.
● For example: ● For example: age = 45.
String str = new String();
● It sets the initial value of
● It creates an instance of the variable age to 45.
String class.

11
Instructor – Zaheredin S.
Cont…
Declaring a Variable to Refer to an Object
● Previously, you learned that to declare a variable, you
write: type name;
● This notifies the compiler that you will use name to
refer to data whose type is type. With a primitive
variable, this declaration also reserves the proper
amount of memory for the variable.
● You can also declare a reference variable on its own
line. For example:
Point originOne;

12
Instructor – Zaheredin S.
Cont…
● If you declare originOne like this, its value will be
undetermined until an object is actually created and
assigned to it.
● Simply declaring a reference variable does not create an
object. For that, you need to use the new operator, as
described in the next section.
● You must assign an object to originOne before you use
it in your code. Otherwise, you will get a compiler error.

13
Instructor – Zaheredin S.
4.4 - Instance fields, Constructors and
methods
1. Instance variable in Java
● A variable that is created inside the class but outside
the method, is known as instance variable.
● Instance variable doesn't get memory at compile time.
● It gets memory at runtime when object(instance) is
created. That is why, it is known as instance variable.
2. Method in Java
● In java, a method is like function i.e. used to represent
behaviour of an object.
14
Instructor – Zaheredin S.
Cont…
● A method is a block of code or collection of statements
or a set of code grouped together to perform a certain
task or operation.
Advantage of Method
○ Code Reusability
○ Code Optimization

15
Instructor – Zaheredin S.
Cont…
3. Constructors
● in java, constructor is a special type of method that is
used to initialize the object.
● It is invoked at the time of object creation.
● It constructs the values i.e. provides data for the object
that is why it is known as constructor.
● Every class has a constructor.
● If we do not explicitly write a constructor for a class,
the Java compiler builds a default constructor for that
class.

16
Instructor – Zaheredin S.
Cont…
● Each time a new object is created, at least one
constructor will be invoked.
● A class can have more than one constructor.
● A constructor is called automatically when a new
instance of an object is created.
Here’s the basic format for coding a constructor:
public ClassName (parameter-list) {
statements...
}

17
Instructor – Zaheredin S.
Cont…
● Constructors have one purpose in life: to create an
instance of a class. This can also be called creating an
object.
● The purpose of methods is much more general.
● A method's basic function is to execute Java code.

18
Instructor – Zaheredin S.
Cont…
Example of a constructor is given below:
class Puppy{
public Puppy(){

}
public Puppy(String name){
// This constructor has one parameter, name.
}
}
● Java also supports Singleton Classes where you would be
able to create only one instance of a class.
19
Instructor – Zaheredin S.
Cont…
There are two types of constructors
 Default constructor (no-arg constructor)
 Parameterized constructor
I. Java Default Constructor
● A constructor that have no parameter is known as
default constructor.
● Syntax of default constructor:
<class_name>(){

}
20
Instructor – Zaheredin S.
Cont…
Example of default constructor
class Dog1{
Dog1(){
System.out.println(“Dog is created");
}
public static void main(String args[]){
Dog1 d = new Dog1();
} Output
} Dog is created

21
Instructor – Zaheredin S.
Cont…
● Default constructor provides the default values to the
object like 0, null etc. depending on the type.
class Student{ Output
int id; 0 null
String name;
void Display(){
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student s=new Student();
s.Display();
}}
22
Instructor – Zaheredin S.
Cont…
● Explanation: In the above class, you are not creating any
constructor so compiler provides you a default
constructor. Here 0 and null values are provided by
default constructor.
II. Java parameterized constructor
● A constructor that have parameters is known as
parameterized constructor.
Why use parameterized constructor?
● Parameterized constructor is used to provide different
values to the distinct objects. Example:

23
Instructor – Zaheredin S.
Cont…
class Student{ Output
int id; 111 tommy
String name;
public Student(int i, String n){
id = i;
name = n;
System.out.println(id+" "+name);
}
public static void main(String []args){
Student std = new Student(111, "tommy");
}
}
24
Instructor – Zaheredin S.
Difference between constructor and method
Constructor Method
● It is used to initialize the ● It is used to expose
state of an object. behaviour of an object.
● Must not have return type. ● Method must have return
● It is invoked implicitly. type.
● The java compiler provides a ● Method is invoked
default constructor if you explicitly.
don't have any constructor. ● It is not provided by
● Constructor name must be compiler in any case.
same as the class name. ● Method name may or may
not same as class name.
25
Instructor – Zaheredin S.
Cont…
Accessing Instance Variables and Methods
● Instance variables and methods are accessed via
created objects.
● To access an instance variable and method the fully
qualified path should be as follows:
// First create an object
ObjectReference = new Constructor ();
// Now call a variable as follows
ObjectReference.variableName;
// Now you can call a class method as follows
ObjectReference.MethodName();
26
Instructor – Zaheredin S.
Cont…
Example to access instance variables & methods.
class Puppy{
int puppyAge;
public Puppy(String name){
System.out.println("Passed Name is :" + name );
}
public void setAge( int age ){
puppyAge = age;
}
public int getAge( ){
return puppyAge;
}
27
Instructor – Zaheredin S.
Cont…
public static void main(String []args){
Puppy p = new Puppy( "tommy" );
//Call class method to set puppy's age
p.setAge( 2 );
//Call another class method to get puppy's age
p.getAge( );
//You can access instance variable as follows as well
System.out.println(" Puppy's age is :" + p.puppyAge);
}
} Output
Passed Name is :tommy
Puppy's age is :2

28
Instructor – Zaheredin S.
END
Thank You!

You might also like