OOP
Object-Oriented Programming
Class, Object

Class
public class Person {
public String name; // attribute
public int age;
public Person(String name, int age) { // constructor
this.name = name;
this.age = age;
}
public String toString() { // method
return "Name: " + name + ", Age: " + age;
}
}
Object
Person person = new Person("Hiep", 20);
System.out.println(person.toString());
4 main principles of OOP
public class Person {
private String name; // attribute
private int age;
public Person() {} // constructor (no parameter)
public Person(String name, int age) { // constructor
this.name = name;
this.age = age;
}
public String toString() { // method
return "Name: " + name + ", Age: " + age;
}
public String getName() { // getter (get method)
return this.name;
}
public void setName(String name) { // setter (set method)
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}
Encapsulation
- Hide data (attribute) with
private - Use
publicget and set methods to access data
Modifiers (private, public, ...)
Keyword that is used to change the behavior of a class, method, variable.
Inheritance
Use extends to inherit all the properties and methods of the superclass
public class Student extends Person {
private String major;
private int grade;
public Student(String name, int age, String major, int grade) {
super(name, age);
this.major = major;
this.grade = grade;
}
public void study() {
System.out.println(getName() + " is studying " + major);
}
public void takeExam() {
System.out.println(getName() + " is taking an exam");
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
Polymorphism
Ability of an object to take on many forms
1. Overriding
Subclass can provide its own implementation of a method that is already defined in its superclass
public class Student extends Person {
// ...
public String getName() { // inherited from the Person class
return "Student: " + this.name;
}
@Override
public String toString() { // inherited from the Object class
return "Student {" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", major='" + major + '\'' +
", grade=" + grade +
'}';
}
}
2. Overloading
Same name, different parameters
public class Calculator {
public int add(int x, int y) {
return x + y;
}
public double add(double x, double y) {
return x + y;
}
public int add(int x, int y, int z) {
return x + y + z;
}
}
Abstraction
Exposing the essential features.
- Abstract class: class that cannot be instantiated
- Abstract method: method does not have a body
Subclass must provide an implementation for all the abstract methods defined in the superclass.
public abstract class Person {
protected String name;
protected int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public abstract void introduce();
public void printInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Student extends Person {
private String major;
public Student(String name, int age, String major) {
super(name, age);
this.major = major;
}
public void introduce() {
System.out.println("Hi, my name is " + name + " and I'm a student majoring in " + major + ".");
}
public void printInfo() {
super.printInfo();
System.out.println("Major: " + major);
}
}
Person person = new Student("Hiep", 26); // Not working
Student student = new Student("Ha", 20, "Japanese");
student.introduce(); // prints "Hi, my name is Ha and I'm a student majoring in Japanese."
Interface
- Defines a set of method signatures that must be implemented by any class that implements the interface
- Completely abstract class that is used to group related methods with empty bodies
public interface IMilitaryStatus {
boolean canServeInTheMilitary();
}
Using implements to implement this interface
public class Person implements IMilitaryStatus {
// ...
@Override
public boolean canServeInTheMilitary() {
return true;
}
}
public class Student extends Person {
// ...
@Override
public boolean canServeInTheMilitary() {
return false;
}
}
Person person = new Person("Hiep", 26);
Student student = new Student("Ha", 20, "Japanese", 10);
System.out.println(person.canServeInTheMilitary()); // true
System.out.println(student.canServeInTheMilitary()); // false
