Oriented Object Programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. It is based on the concept of objects, which contain data and behavior.


Java, being an object-oriented programming language, implements the OOP concept with the following properties:

  1. Package: In Java, a package is a collection of classes and interfaces that provide a way to organize and structure code. To create a package, you can use the package keyword followed by the package name. For example, package com.example;. To access the classes in a package, you need to import the package using the import keyword. For example, import com.example.*;.
  2. Object: An object in Java is an instance of a class and represents a real-world entity. An object contains state and behavior. The components of an object are instance variables and methods. To create an object in Java, you can use the new operator followed by the class constructor. For example, MyClass obj = new MyClass();.
  3. Class: A class in Java is a blueprint or template for creating objects. It defines the data and behavior of objects of its type. The components of a class are instance variables, methods, and constructors. To create a class in Java, you need to use the class keyword followed by the class name. For example, class MyClass { ... }.
Instance of a classBlueprint or template
Represents a real-world entityDefines the data and behavior
Contains state and behaviorContains instance variables, methods, and constructors
  1. Access Modifier: In Java, access modifiers are keywords that define the visibility of a class, method, or variable. There are four access modifiers: public, private, protected, and default. To define the access level of a class, method, or variable, you need to use the access modifier keyword before the declaration. For example, private int myVariable;.
  2. Inheritance: Inheritance is a mechanism in Java where one class inherits the properties of another class. This allows the creation of new classes that are derived from existing classes, resulting in the reuse of code and promoting the principle of code reuse. To implement inheritance in Java, you need to use the extends keyword followed by the parent class name. For example, class ChildClass extends ParentClass { ... }.
  3. Encapsulation: Encapsulation is a mechanism in Java that allows hiding the implementation details of a class and exposing only the necessary information to the outside world. This promotes the idea of data hiding and makes the code more secure and maintainable. To implement encapsulation in Java, you need to declare the class variables as private and provide public accessor and mutator methods. For example, private int myVariable; and public int getMyVariable() { return myVariable; }.
  4. Polymorphism: Polymorphism is a mechanism in Java that allows objects of different classes to be treated as objects of the same type. This allows the implementation of methods with the same name but different behavior. To implement polymorphism in Java, you need to use method overriding and method overloading. Method overriding is the ability of a subclass to provide a different implementation of a method inherited from its parent class. Method overloading is the ability of a class to have multiple methods with the same name but different parameters.
  5. Abstraction: Abstraction is a mechanism in Java that allows hiding the complex implementation details of a class and exposing only the necessary information to the outside world. This promotes the idea of modularity and makes the code more maintainable. To implement abstraction in Java, you can use abstract classes and interfaces. An abstract class is a class that cannot be instantiated and is meant to be extended by other classes. An interface is a collection of abstract methods that are meant to be implemented by other classes. To declare an abstract class, you need to use the abstract keyword before the class declaration. To declare an interface, you need to use the interface keyword.



  • Reusability of code
  • Improved code maintainability
  • Improved security
  • Improved modularity
  • Improved abstraction


  • Overhead of creating and managing objects
  • Increased complexity


Novatesting would like to give you the following sample exercise so that you can get deeper into Java OOP knowledge.


  1. Create a class Person with private fields firstName, lastName, and age.
  2. Implement getter and setter methods for each field.
  3. Create a subclass Student that extends the Person class.
  4. Add a private field studentId to the Student class and implement its getter and setter methods.
  5. Override the toString method in both classes to display the information of a person and a student in the following format: “Name: firstName lastName, Age: age, Student ID: studentId”.
  6. Create a class Main with a main method.
  7. In the main method, create an instance of the Student class and set its properties using the setter methods.
  8. Print the information of the student using the toString method.

Answer to the Exercise:

  1. The Person class:
public class Person {
    private String firstName;
    private String lastName;
    private int age;
    public String getFirstName() {
        return firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    public String getLastName() {
        return lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;
    public int getAge() {
        return age;
    public void setAge(int age) {
        this.age = age;
    public String toString() {
        return "Name: " + firstName + " " + lastName + ", Age: " + age;
  1. The Student class:
public class Student extends Person {
    private int studentId;
    public int getStudentId() {
        return studentId;
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    public String toString() {
        return "Name: " + getFirstName() + " " + getLastName() + ", Age: " + getAge() + ", Student ID: " + studentId;
  1. The Main class:
public class Main {
    public static void main(String[] args) {
        Student student = new Student();


Name: John Doe, Age: 22, Student ID: 123456

This exercise demonstrates the basic concepts of Java OOP such as inheritance, encapsulation, and polymorphism. The Person class represents the basic information of a person and the Student class extends the Person class to include additional information such as the student ID. The Main class creates an instance of the Student class, sets its properties, and prints the information of the student using the toString method. This exercise provides hands-on practice on how to create a class hierarchy, use inheritance, and access class properties using getter and setter methods. By creating an instance of the Student class and using the toString method, students can see how polymorphism works in practice.

This exercise can be extended further by adding more methods or classes to the hierarchy, providing students with more opportunities to practice and strengthen their understanding of OOP in Java.

In conclusion, OOP is a powerful programming paradigm that offers many benefits for software development, and Java provides a strong implementation of OOP concepts. However, like any tool, it’s essential to understand the advantages and limitations and use it appropriately.

By admin1

Leave a Reply

Your email address will not be published. Required fields are marked *