DEV Community

Adeyemi Raji
Adeyemi Raji

Posted on

OOP Concept in Java explained in simple terms

In this example, the Person class has a constructor that takes a single String argument and assigns it to the name field of the object. The class also has a getName method that returns the value of the name field. We can create an object of the Person class using the constructor, and then use the getName method to retrieve the value of the name field.

public class Person {
  private String name;
  private int age;

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

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

  public String getName() {
    return name;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getAge() {
    return age;
  }
}

Enter fullscreen mode Exit fullscreen mode

In this example, the Person class has two private fields: name and age. The class has four public methods: a constructor, and three getter and setter methods for the name and age fields.

To create an object of the Person class, we can use the new keyword and pass the required arguments to the constructor:

Person person = new Person("John", 30);

Enter fullscreen mode Exit fullscreen mode

We can then use the object's methods to get and set the values of its fields:

String name = person.getName();  // name is "John"
int age = person.getAge();  // age is 30
person.setName("Jane");
person.setAge(35);

Enter fullscreen mode Exit fullscreen mode

OOP languages, such as Java, also support inheritance, which allows a class to inherit the properties and behaviors of another class. This allows developers to create a hierarchy of classes, where a subclass can inherit the characteristics of its superclass, and can also add new properties and behaviors of its own.

public class Employee extends Person {
  private double salary;

  public Employee(String name, int age, double salary) {
    super(name, age);
    this.salary = salary;
  }

  public void setSalary(double salary) {
    this.salary = salary;
  }

  public double getSalary() {
    return salary;
  }
}

Enter fullscreen mode Exit fullscreen mode

In this example, the Employee class extends the Person class and adds a new field called salary. The Employee class has a constructor that calls the superclass constructor using the super keyword and passes the required arguments. The Employee class also has getter and setter methods for the salary field.

OOP languages, such as Java, also support polymorphism, which allows objects of different classes to be treated as instances of a common superclass. This allows developers to write code that can work with multiple classes that have similar characteristics, without having to know the exact type of the object at runtime.
Person person = new Employee("John", 30, 50000);
String name = person.getName(); // name is

In summary, object-oriented programming (OOP) is a programming paradigm that is based on the concept of "objects", which contain data and code that operates on that data. In Java, a class is a template or blueprint for creating objects, and an object is an instance of a class. A class can have fields (data) and methods (code) that define the properties and behaviors of the object. Java also supports inheritance, which allows a class to inherit the properties and behaviors of another class, and polymorphism, which allows objects of different classes to be treated as instances of a common superclass. OOP can help developers create programs that are easy to understand, maintain, and reuse.

Top comments (0)