Object-Oriented Programming in Java

Object in Java

Employee emp = new Employee();
byte       for whole numbers
short for whole numbers
int for whole numbers
long for whole numbers
float for floating numbers
double for floating numbers
char for single character(ACII table)
boolean logical - can be true or false

Object-Oriented Programming in Java

  1. Encapsulation
  2. Inheritance
  3. Abstraction
  4. Polymorphism
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("age cannot be negative");
}
this.age = age;
}
}
  1. Why do we need to encapsulate? If the property has public access, the client code can have direct access and can assign any value. By encapsulating we have one layer where we can control what comes to our property in our setter method. In the above example, we can see how we are restricting negative age in the setAge method. Another example — let’s say we are creating a custom List data structure based on an array. The underlining array data structure should be private if we will make it public, it will be accessible to client code. There are possibilities that client code will create a big mess by manipulating directly with the array.
  2. From whom we are protecting our data? Seems a very simple question however understanding this question is the main part to start developing using the encapsulation concept. We are removing direct access from object properties so we are protecting it from client code that will use this class(object). We are hiding properties from ourselves if we will use this class in other parts of the project.
  3. If the property type of object is a mutable object. We cannot return the original address because the client will direct access using the returned reference. We always need to take a copy and return a reference to it.
  4. Even though you can create your setters and getters methods with any name or if the requirement is not required to have them, it is totally fine to avoid them. But keep in mind that if you want to use your objects with external libraries so they might assume you have all setters and getters with correct names.
// in Person.java file
public class Person {
public String name;
public String address;
public int age;

public void walk() {
System.out.println(name + " is walking.");
}
}
// in Student.java file
public class Student extends Person {
public static void main(String[] args){
Student student = new Student();
student.name = "John Doe";
student.address = "101 Main St";
student.age = 22;
student.walk();
}
}
Student extends Person
equals(Object obj)
In our case Object is grandpa for Student class :)
Java allows only a single inheritance type
// In FileService.java file
public abstract class FileService {
public abstract void saveFile(String source, String target);
public abstract String getFileContent(String source);
public abstract void copyFile(String source, String target);
public abstract boolean deleteFile(String path);
}
// In FileServiceLocalImpl.java
public class FileServiceLocalImpl extends FileService {
@Override
public void saveFile(String source, String target){
// code that will save file into file system
}

@Override
public String getFileContent(String source){
// code that will get file content
}
@Override
public void copyFile(String source, String target){
// code that will copy file
}
@Override
public boolean deleteFile(String path){
// code that will delete file
}
}
public class Main{
public static void main(String[] args) {
// FileService fService = new FileService();
// It will not compile because we cannot create object
// from abstract class directly

FileService fService = new FileServiceLocalImpl();
// ...
// code that use fService do resolve some problem
}
}
FileService fService = new FileServiceLocalImpl();
FileService fService = new FileServiceLocalImpl();
FileService fService = new FileServiceS3Impl();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store