Object and
Class
Computer Engineering
Class
▪ Defines new data type (primitive ones are not enough).
▪ For Example : SmartPhone, person, student
▪ This new types can be used to create objects of that type
▪ So, a class is a template for an object and an object is an instance
of a class
Unit-2 Classes, Objects & Methods
Object
Unit-2 Classes, Objects & Methods
Objects of a same kind….
▪ This phones are instances of a Class SmartPhone
▪ Class stands for a blueprint from which individual objects are
created
Unit-2 Classes, Objects & Methods
Class SmartPhone
class SmartPhone
{
String manufacturer;
String model;
double storage;
double screenSize;
}
Fields represents object’s state
Unit-2 Classes, Objects & Methods
Object
An object is an instance of a class.
A class is a template or blueprint from which objects are created.
So, an object is the instance(result) of a class.
Object Definitions:
•An object is a real-world entity.
•An object is a runtime entity.
•The object is an entity which has state and behavior.
•The object is an instance of a class.
Unit-2 Classes, Objects & Methods
Object
Unit-2 Classes, Objects & Methods
Object
Unit-2 Classes, Objects & Methods
Object
Unit-2 Classes, Objects & Methods
The new operator
▪ Creates a new object
▪ Object is just an instance of a class.
▪ Class is just a logical framework, object is physical reality
▪ Syntax:
ClassName varName = new ClassName();
Unit-2 Classes, Objects & Methods
Dot operator .
▪ Object variables and methods can be accessed using the dot
operator
▪ Example:
SmartPhone iPhone = new SmartPhone();
iPhone.storage = 8000;
Unit-2 Classes, Objects & Methods
Visibility Control
Java provides three types of visibility modifiers: public,
private and protected. They provide different levels of protection as
described below.
Public Access: Any variable or method is visible to the entire class in which it
is defined. But, to make a member accessible outside with objects, we
simply declare the variable or method as public. A variable or method
declared as public has the widest possible visibility and accessible
everywhere.
Friendly Access (Default): When no access modifier is specified, the member
defaults to a limited version of public accessibility known as "friendly"
level of access. The difference between the "public" access and the
"friendly" access is that the public modifier makes fields visible in all
classes, regardless of their packages while the friendly access makes fields
visible only in the same package, but not in other packages.
Unit-2 Classes, Objects & Methods
Protected Access: The visibility level of a "protected" field lies in between the
public access and friendly access. That is, the protected modifier makes
the fields visible not only to all classes and subclasses in the same
package but also to subclasses in other packages
Private Access: private fields have the highest degree of protection. They are
accessible only with their own class. They cannot be inherited by
subclasses and therefore not accessible in subclasses. In the case of
overriding public methods cannot be redefined as private type.
Private protected Access: A field can be declared with two
keywords private and protected together. This gives a visibility level in
between the "protected" access and "private" access. This modifier makes
the fields visible in all subclasses regardless of what package they are in.
Remember, these fields are not accessible by other classes in the same
package. Java did originally have the private protected modifier, but it was
removed in JDK 1.0.2
Unit-2 Classes, Objects & Methods
Access public protected Default private private
(Friendly) protected
modifier 🡪
Own class
✔ ✔ ✔ ✔ ✔
Sub class
in same
package ✔ ✔ ✔ ✔ 🗶
Other
classes
In same ✔ ✔ ✔ 🗶 🗶
package
Sub class
in other
package ✔ ✔ 🗶 ✔ 🗶
Other
classes
In other ✔ 🗶 🗶 🗶 🗶
package
Unit-2 Classes, Objects & Methods
Introducing methods
▪ Classes need to contain data and behavior
▪ Methods represents the behavior of a class
▪ Remember : a Method is always invoked relative to an object. (except ….)
▪ It operates on any object of the class of which it is a member, and has access to all
the members of a class for that object.
Objectname.methodname
▪ Example:
Box b = new Box();
b.width = 10;
b.height = 15;
b.breadth = 20;
double volume = b.getVolume();
Unit-2 Classes, Objects & Methods
Introducing methods
class Lamp
{
boolean isOn;
}
// create object
Lamp l1 = new Lamp();
l1.isOn = true;
Unit-2 Classes, Objects & Methods
Introducing methods
class Lamp
{
boolean isOn;
void turnOn()
{
// initialize variable with value true
isOn = true;
System.out.println("Light on? " + isOn);
}
}
// create object
Lamp l1 = new Lamp();
l1.turnOn();
Unit-2 Classes, Objects & Methods
Introducing methods
Class area have two variable x and y;
Created two objects of Class area A1, A2;
Class area has one method calculatearea() that calculate x*y;
A1 A2
x=20 X=10
Y=30 y=20
A1.caluclatearea() return x*y=20*30 = 600
A2.caluclatearea() return x*y=10*20 = 200
Unit-2 Classes, Objects & Methods
19
Unit-2 Classes, Objects & Methods
20
Unit-2 Classes, Objects & Methods
21
Unit-2 Classes, Objects & Methods
Get and Set method in Class (methods)
public class SmartPhone {
String manufacturer;
String model;
double storage;
double screenSize;
public String getManufacturer()
{
return manufacturer;
}
public void setManufacturer(String a)
{
manufacturer = a;
}
}
22
Unit-2 Classes, Objects & Methods
Method Overloading
• If a class has multiple methods having same
name but different in parameters, it is known
as Method Overloading.
• There are two ways to overload the method in
java,
• By changing number of arguments
• By changing the data type
• In java, Method Overloading is not possible by
changing the return type of the method only.
Unit-2 Classes, Objects & Methods
Method Overloading
Example:
class Adder{
static int add(int a,int b){return a+b;}
static float add(float a,float b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11.0f,11.0f));
System.out.println(Adder.add(11,11,11));
}}
Unit-2 Classes, Objects & Methods
Nesting of Methods
When a method in java calls another method in the
same class, it is called Nesting of methods.
The below Java Program to Show the Nesting of
Methods.
Unit-2 Classes, Objects & Methods
import java.util.Scanner; public static void main(String[] args)
public class Nest_Methods {
{ Scanner s = new Scanner(System.in);
int area(int l, int b)
{ System.out.print("Enter length of cuboid:");
int ar = 6 * l * b; int l = s.nextInt();
return ar;
} System.out.print("Enter breadth of cuboid:");
int volume(int l, int b, int h) int b = s.nextInt();
{
int ar = area(l, b); System.out.print("Enter height of cuboid:");
System.out.println("Area:"+ar); int h = s.nextInt();
int vol ;
vol = l * b * h; Nest_Methods obj = new Nest_Methods();
return vol; int vol = obj.volume(l, b, h);
} System.out.println("Volume:"+vol);
}
}
Unit-2 Classes, Objects & Methods
Passing and Returning Objects in Java
• When we pass a primitive type to a method, it is
passed by value.
• When we pass an object to a method, the
situation changes dramatically, because objects
are passed by what is effectively call-by-
reference.
Unit-2 Classes, Objects & Methods
Constructor
▪ A constructor in Java is a block of code similar to a method that’s
called when an instance of an object is created.
▪ It is used to initialized the data member of objects.
▪ Here are the key differences between a constructor and a method:
• A constructor doesn’t have a return type(not even void).
• The name of the constructor must be the same as the name of the class.
• Unlike methods, constructors are not considered members of a class.
• A constructor is called automatically when a new instance of an object is
created.
• It is a special type of method which is used to initialize the object.
• Constructors cannot be private.
• An interface cannot have the constructor.
28
Unit-2 Classes, Objects & Methods
Constructor
▪ A Java constructor cannot be abstract, static, final, and synchronized
▪ It calls a default constructor if there is no constructor available in the
class. In such case, Java compiler provides a default constructor by
default.
▪ Syntax
▪ Example
public ClassName (parameter-list)
{
statements;
}
29
Unit-2 Classes, Objects & Methods
Constructor
▪ Constructor can be of two types:
▪ Default Constructor
▪ Parameterized Constructor
▪ Example of Default Constructor
public ClassName ()
{
statements...
}
▪ Example of Parameterized Constructor
public ClassName (parameter-list)
{
statements...
}
Unit-2 Classes, Objects & Methods
Constructor Overloading
public class Human {
double noOfBreath;
double luck;
String name;
public Human()
{
noOfBreath = 1000;
luck = 0.5;
}
public Human(String n)
{
name = n;
}
public Human(double b, double l, String n){
noOfBreath = b; luck = l; name = n;
}
} 31
Unit-2 Classes, Objects & Methods
Unit-2 Classes, Objects & Methods
Copy Constructor
▪ In Java, there is no such concept like copy constructor as we are
having in C++ language. But, we can implement by using following
two ways:
▪ By constructor
▪ By clone() method of Object class
Unit-2 Classes, Objects & Methods
Copy Constructor
public class Human {
double noOfBreath;
String name;
public Human(double b, String n)
{
noOfBreath = b;
name = n;
}
public Human(Human h)
{
noOfBreath = h.noOfBreath;
name = h.name;
}
}
Unit-2 Classes, Objects & Methods
this keyword
• this is a reference variable that refers to the
current object.
Unit-2 Classes, Objects & Methods
Example: to refer current class instance
variable
public class SmartPhone {
String manufacturer;
String model;
double storage;
double screenSize;
public void setManufacturer(String manufacturer)
{
this.manufacturer = manufacturer;
}
public String getManufacturer()
{
return manufacturer;
}
}
Unit-2 Classes, Objects & Methods
Example (methods) using this operator
public class SmartPhone {
String manufacturer;
String model;
double storage;
double screenSize;
public void printManufacturer()
{
System.out.println(“Value of Manufacturer”+manufacturer);
}
public void printModel()
{
this.printManufacturer();//same as printManufacturer();
System.out.println(“Value of Model: ”+model);
}
}
37
Unit-2 Classes, Objects & Methods
Example: to invoke current class constructor
public class Human {
double noOfBreath;
double luck;
String name;
public Human()
{
noOfBreath = 1000;
luck = 0.5;
}
public Human(String name)
{
this();
this.name = name;
}
}
Unit-2 Classes, Objects & Methods
varargs: variable-length arguments
⮚ The varargs allows the method to accept zero or multiple
arguments.
⮚ If we don't know how many argument we will have to pass in the
method, varargs is the better approach.
Syntax of varargs:
return_type method_name(data_type... variableName){}
Unit-2 Classes, Objects & Methods
class VarargsExample
{
static void display(String... values)
{
System.out.println("display method invoked ");
for(String s:values)
{ Output:
System.out.println(s); display method invoked
display method invoked
}
hello
} display method invoked
My
public static void main(String args[]) Name
Is
{ varargs
display();//zero argument
display("hello");//one argument
display("my","name","is","varargs");//four arguments
}
}
Unit-2 Classes, Objects & Methods