Chapter - 2 - Objects and Classes Full
Chapter - 2 - Objects and Classes Full
}
void sleeping(){
//body .....
}
Creating Objects
• A class provides the blueprints for objects.
Local variable
Instance variable
Class variable
Local Variables
Local variables are declared in methods,
constructors, or blocks.
Local variables are created when the method,
constructor or block is entered and the variable will be
destroyed once it exits the method, constructor or
block.
Access modifiers cannot be used for local variables.
Local variables are visible only within the declared
scope.
There is no default value for local variables so local
variables should be declared and an initial value should
be assigned before the first use.
Local Variable example
the class.
Instance variables have default values. For numbers the default value is 0, for
variables are given accessibility) should be called using the fully qualified
name. ObjectReference.VariableName.
public class Employee { Instance Variable
// This instance variable is visible to all classes
public String name; example
// Salary variable is visible only in Employee class
private double salary;
// The name variable is assigned a value in the
constructor.
public Employee(String empName){
name = empName;
}
// The salary variable is assigned a value
public void setSalary(double empSal){
salary = empSal;
}
// This method prints employee detail
public void printEmp(){
System.out.println("Name: " + name);
System.out.println("Salay: " + salary);
}
public static void main(String[] args) {
Employee empOne = new Employee("Daniel");
empOne.setSalary(10000.0);
empOne.printEmp();
}
Class/static variables:
Class variables known as static variables are declared with
the static keyword in a class, but outside of any method, constructor or
any block.
Static variables are created when the program starts and destroyed
when the program stops.
Visibility is similar to instance variables.
if (str1 == str2) {
System.out.println("The strings have the same
reference");
}
Destroying Objects [Reading assignment]
• In Java, objects are automatically destroyed by the
garbage collector when they are no longer referenced by
any part of the program. However, you can explicitly
destroy an object by setting its reference to null.
Outside
Access Same Same package by Outside
Modifier Class Package subclass only Package
public Yes Yes Yes Yes
private Yes No No No
Encapsulation
• Refers to the bundling of data and methods into a single unit, and
controlling access to that unit to prevent unwanted modifications or access
by other parts of the program.
• This approach ensures that the object's state can only be modified in
a controlled and consistent manner, which can help prevent errors
and improve the reliability of the code.
set (i.e., assign values to) or get (i.e., obtain the values of)
private instance variables.
– Set methods are also commonly called mutator methods,
because they typically change an object’s state. i.e., modify the
values of instance variables.
– Get methods are also commonly called accessor methods or
query methods.
See PoorDog.java & PoorDogTestDrive.java
class Student { Application with
private String ID; private String fName;
two classes
private String lName;
private String gender;
private int age;
public void setId(String idn){ID= idn;}
public void setFirstName(String fname){fName =
fname;}
public void setLastName(String lname){ lName =
lname;}
public void setAge(int a){age = a;}
public void setGender(String sex){gender = sex;}
}
}
Rules of using Constructors
Default Constructors
No-argument Constructors
Parameterized Constructors
Default Constructors
If you do not implement any constructor in your class , Java compiler
inserts a default constructor into your code on your behalf.
By default, the compiler provides a default constructor with no
parameters in any class that does not explicitly include a constructor.
When a class has only the default constructor, its instance
variables are initialized to their default values.
Compiler creates
default constructor
• If you declare any constructor for a class, the Java
compiler will not create a default constructor for that
class.
• The body of default constructor is empty.
No-argument Constructor
}
Constructor Overloading
• It is possible to have more than one constructors in one program.
• Constructor overloading is a concept of having more than one constructor
with different parameters list.
public class Demo{
Constructor without
Demo(){
Argument
//…….
}
Demo(String s){
Constructor with
//……..
String Argument
}
Demo(int i){
//…….
Constructor with int
}
Argument
}
Quiz
• Create a Java class called "Person" with the following
properties:
– A private string variable called "name" to store the person's
name
– A private integer variable called "age" to store the person's age
• The class should also have the following methods:
– A public constructor that takes two arguments: a string for the
person's name and an integer for the person's age. The
constructor should initialize the name and age properties.
– Public getters and setters for both the name and age properties.
• In the main method of a separate class, create an instance
of the Person class and prompt the user to enter a name
and age for the person. Use the setters to set the name