Introduction to Object-Oriented Programming (OOP)

Introduction to OOP concepts

Object-Oriented Programming (OOP) is a programming paradigm that uses objects to represent real-world entities and their interactions. OOP emphasizes concepts such as encapsulation, inheritance, and polymorphism to organize code and make it more reusable and modular.

Classes and objects

In Java, a class is a blueprint or template for creating objects. An object is an instance of a class and represents a specific instance of the data and behaviors defined in the class.

Here's an example of a simple class in Java:

public class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
}

In this example, we define a Person class with two private instance variables name and age, and a constructor that takes two arguments to initialize these variables.

We also define two getter methods, getName() and getAge(), which returns the values of the name and age instance variables, respectively.

To create an instance of this class, we use the new keyword to allocate memory for the object and call the constructor:

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

Now p refers to a newly-allocated Person object with the name "John" and age 30.

Encapsulation, inheritance, and polymorphism

Encapsulation, inheritance, and polymorphism are three key concepts in OOP that help organize code and make it more modular and reusable.

Encapsulation refers to the practice of hiding the internal implementation details of a class from external code and providing a public interface for interacting with the class. In Java, we can use access modifiers like public, private, and protected to control the visibility of class members.

Inheritance is a mechanism that allows a new class to be based on an existing class, inheriting its data and behavior. In Java, we use the extends keyword to create a subclass that inherits from a superclass.

Polymorphism is the ability of an object to take on many forms. In Java, polymorphism is achieved through method overriding and method overloading.

Here's an example of inheritance in Java:

public class Student extends Person {
    private int id;
    
    public Student(String name, int age, int id) {
        super(name, age);
        this.id = id;
    }
    
    public int getId() {
        return id;
    }
}

In this example, we define a Student the class that extends the Person class. The Student the class adds a new instance variable id, and a constructor that takes three arguments to initialize name, age, and id.

We also define a getId() the method that returns the value of id.

Since Student extends Person, it inherits all the data and behavior defined in Person, including the name and age instance variables, and the getName() and getAge() methods.

That covers the basics of OOP in Java!

Last updated