Object-oriented Programming (OOP)
What is object-oriented programming?
Object-oriented programming (OOP) focuses on some aspects which are classes, objects, methods, and attributes. An example is given below to structure an OOP program using Java programming.
Building a class…
public class Student{
}
Providing attributes
public class Student{
private String name;
private String id;
public String country;
public String contact_num;
public int date_birth;
public String course_name;
public double cgpa;
}
Providing constructors with & without parameters
A constructor without parameter initializes the value of attributes but a constructor with parameters assigns the value that is passed by parameters to attributes.
public class Student{
private String name;
private String id;
public String country;
public String contact_num;
public int date_birth;
public String course_name;
public double cgpa;
/*constructor without parameter*/
public Student(){
this.name = "Patrick Anderson";
this.id = "4856749356733454";
this.country = "Malaysia";
this.contact_num = "14256175832";
this.date_birth = 99991201;
this.course_name = "Computer Science";
this.cgpa = 3.96;
}
/*constructor with parameter*/
public Student(String name, String id, String country,
String contact_num, int date_birth,
String course_name, double cgpa){
this.name = name;
this.id = id;
this.country = country;
this.contact_num = contact_num;
this.date_birth = date_birth;
this.course_name = course_name;
this.cgpa = cgpa;
}
}
Providing Setters and Getters methods
Setters method is to write the value of the attribute whereas the Getters method is to read the value of the attribute. Normally, the Setters method is named starting with set but the Getters method is named starting with get.
public class Student{
private String name;
private String id;
public String country;
public String contact_num;
public int date_birth;
public String course_name;
public double cgpa;
/*constructor without parameter*/
public Student(){
this.name = "Patrick Anderson";
this.id = "4856749356733454";
this.country = "Malaysia";
this.contact_num = "0123456789";
this.date_birth = 99991201;
this.course_name = "Computer Science";
this.cgpa = 3.96;
}
/*constructor with parameter*/
public Student(String name, String id, String country,
String contact_num, int date_birth,
String course_name, double cgpa){
this.name = name;
this.id = id;
this.country = country;
this.contact_num = contact_num;
this.date_birth = date_birth;
this.course_name = course_name;
this.cgpa = cgpa;
}
/*Getters and Setters methods*/
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getContact_num() {
return contact_num;
}
public void setContact_num(String contact_num) {
this.contact_num = contact_num;
}
public int getDate_birth() {
return date_birth;
}
public void setDate_birth(int date_birth) {
this.date_birth = date_birth;
}
public String getCourse_name() {
return course_name;
}
public void setCourse_name(String course_name) {
this.course_name = course_name;
}
public double getCgpa() {
return cgpa;
}
public void setCgpa(double cgpa) {
this.cgpa = cgpa;
}
}
Creating an object for a class
An object is an instance of a class. The object is created with specifically defined data and called methods to perform the relevant operations.
public class MainActivity{
public static void main(String[] args){
/*Create an object for the Student class without parameters*/
Student student1 = new Student();
}
}
An object named student1 is created for the Student class. The attributes of the object hold the value as defined in the constructor without parameters.
Now, let's create an object with parameters
public class MainActivity{
public static void main(String[] args){
/*Create an object for the Student class without parameters*/
Student student1 = new Student();
/*Create an object for the Student class with parameters*/
Student student2 = new Student("Chris James", "2354546312686958",
"United State", "14256175832", 99991201, "Criminal Science", 3.55);
}
}
An object named student2 is created by passing parameters to the constructor. The values that are passed through parameters will be assigned to attributes respectively.
Calling methods from class
public class MainActivity{
public static void main(String[] args){
/*Create an object for the Student class without parameters*/
Student student1 = new Student();
/*Create an object for the Student class with parameters*/
Student student2 = new Student("Chris James", "2354546312686958",
"United State", "14256175832", 99991201, "Criminal Science", 3.55);
/*Call getters method*/
System.out.println("The first student.");
System.out.println("Name: " + student1.getName());
System.out.println("Course name: " + student1.getCourse_name());
System.out.println("Result: " + student1.getCgpa());
System.out.println("The second student.");
System.out.println("Name: " + student2.getName());
System.out.println("Course name: " + student2.getCourse_name());
System.out.println("Result: " + student2.getCgpa());
}
}
The output will be as follows,
The first student.
Name: Patrick Anderson
Course name: Computer Science
Result: 3.96
The second student.
Name: Chris James
Course name: Criminal Science
Result: 3.55
The value of the attribute can be changed by using the setters method.
public class MainActivity{
public static void main(String[] args){
/*Create an object for the Student class without parameters*/
Student student1 = new Student();
/*Create an object for the Student class with parameters*/
Student student2 = new Student("Chris James", "2354546312686958",
"United State", "14256175832", 99991201, "Criminal Science", 3.55);
/*Call setters method*/
student1.setName("Patrick Leon");
student1.setCgpa( 4.00 );
/*Call getters method*/
System.out.println("The first student.");
System.out.println("Name: " + student1.getName());
System.out.println("Course name: " + student1.getCourse_name());
System.out.println("Result: " + student1.getCgpa());
System.out.println("The second student.");
System.out.println("Name: " + student2.getName());
System.out.println("Course name: " + student2.getCourse_name());
System.out.println("Result: " + student2.getCgpa());
}
}
The output will be changed as follows,
The first student.
Name: Patrick Leon
Course name: Computer Science
Result: 4.00
The second student.
Name: Chris James
Course name: Criminal Science
Result: 3.55
Apart from setters and getters methods, any method can be defined to perform relevant and desired operations. Let’s add new methods in Student class to calculate and display CGPA%.
public class Student{
private String name;
private String id;
public String country;
public String contact_num;
public int date_birth;
public String course_name;
public double cgpa;
/*constructor without parameter*/
public Student(){
this.name = "Patrick Anderson";
this.id = "4856749356733454";
this.country = "Malaysia";
this.contact_num = "0123456789";
this.date_birth = 99991201;
this.course_name = "Computer Science";
this.cgpa = 3.96;
}
/*constructor with parameter*/
public Student(String name, String id, String country,
String contact_num, int date_birth,
String course_name, double cgpa){
this.name = name;
this.id = id;
this.country = country;
this.contact_num = contact_num;
this.date_birth = date_birth;
this.course_name = course_name;
this.cgpa = cgpa;
}
/*Getters and Setters methods*/
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getContact_num() {
return contact_num;
}
public void setContact_num(String contact_num) {
this.contact_num = contact_num;
}
public int getDate_birth() {
return date_birth;
}
public void setDate_birth(int date_birth) {
this.date_birth = date_birth;
}
public String getCourse_name() {
return course_name;
}
public void setCourse_name(String course_name) {
this.course_name = course_name;
}
public double getCgpa() {
return cgpa;
}
public void setCgpa(double cgpa) {
this.cgpa = cgpa;
}
/*Method to calculate CGPA%*/
public double cal_CgpaPercent(){
return (this.cgpa * 9.5);
}
/*Method to display CGPA%*/
public void dis_CgpaPercent(double cgpa_percentage){
System.out.println("CGPA%: " + cgpa_percentage + "%");
}
}
Implementing it…
public class MainActivity{
public static void main(String[] args){
/*Create an object for the Student class without parameters*/
Student student1 = new Student();
/*Create an object for the Student class with parameters*/
Student student2 = new Student("Chris James", "2354546312686958",
"United State", "14256175832", 99991201, "Criminal Science", 3.55);
/*Call method*/
double cgpa_Percentage = student1.cal_CgpaPercent();
System.out.println("The first student.");
student1.dis_CgpaPercent(cgpa_Percentage);
cgpa_Percentage = student2.cal_CgpaPercent();
System.out.println("The second student.");
student2.dis_CgpaPercent(cgpa_Percentage);
}
}
The variable cgpa_Percentage is declared to receive the value returns by method cal_CgpaPercent(), and is passed to method dis_CgpaPercent(double) as a parameter to be displayed.
The output will be as follows,
The first student.
CGPA%: 37.62%
The second student.
CGPA%: 33.73%