You are on page 1of 5

• Constructor

- is used in the creation of an object that is an instance of a class using the new keyword

Example:

Employee emp = new Employee();

o A constructor performs operations required to initialize the class before methods are
invoked or fields are accessed
Example:

public Employee() { salary = 15000;


}

Explanation: Every Employee object created will have a default starting salary of
15000 per month.
o Constructors are never inherited
o Constructor declarations use the name of the class and have no return type

• default constructor
- a constructor with an empty parameter list and body
- This is invisibly added to the class
- also known as a no-argument constructor

Example:

public Employee() { }

• this
- is used when the instance variable has the same name with the constructor’s parameter

Example:

public class Employee {


private double salary;
public Employee(double salary) {
this.salary = salary;
}

}
• Constructor overloading
- occurs when constructors have different type parameters

Example:
public class Student {
private String name;
private int age;
public Student() {
name = "No name yet."; age = 0;
}

public Student(String name, int age) {


this.name = name;
this.age = age;

public static void main(String[] args) {


Student s1 = new Student();
System.out.println(s1.name + ", " + s1.age);
s1.name = "Riven Reyes";
s1.age = 17;
System.out.println(s1.name + ", " + s1.age);
Student s2 = new Student("Nika Pena", 18);
System.out.println(s2.name + ", " s2.age);
}
}

Explanation:

Instances of the Student class can be created with or without arguments. Student s1 =
new Student(); calls the default constructor because it does not have arguments

• constructor chaining
- calls another constructor with a greater number of parameters
- accomplished using the this keyword too
- this() call must be the first statement in the constructor

Example:
public Student() { this("No name yet.");
}

public Student(String name) { this(name, 0);


}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
• Encapsulation
- describes the ability of an object to hide its data and methods
• Benefits of encapsulation:
o It allows writing of reusable programs
o It restricts access only to those features of an object that are declared public
• In Java
- a class encapsulates the fields, which holds the state of an object, and the methods,
which define the actions of the object
• Fields
- are encapsulated by declaring instance variables as private
and methods as public
• accessor / getter method
- a public method that returns data from a private instance variable
• mutator / setter method
- a public method that changes the data stored in one or more private instance
variables

Example:

public class Student {


private String name;
public void setName(String name) {
this.name = name;
}

public String getName() {


return name;
}

}
Rules in Implementing Encapsulation

1. Instance variables are declared private


Example:
private String name;

2. Names of getter methods begin with get if the property is not a Boolean
Example:
public String getName() {
return name;
}

3. Names of getter methods begin with is if the property is a Boolean


Example:
public boolean isEnrolled() {
return status;
}

4. Names of setter methods begin with set


Example:
public void setName(String name) {
this.name = name;
}

• Immutable
- if it remains unchanged after an object of another class is constructed
• For a class to be immutable
- remove the setter methods and use the constructor for setting the values
Example:
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}

}
• Inheritance
- allows a class to acquire all the attributes (fields) and behaviors (methods) of another class

• Subclass / derived class / child class


- A class that is derived from another class

• Superclass / base class / parent class


- The class from which the subclass is derived

• Object
- Where all classes in java inherit from

You might also like