DEFINITION
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.
PROPERTIES OF JAVA OOP
Java, being an object-oriented programming language, implements the OOP concept with the following properties:
- 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 theimport
keyword. For example,import com.example.*;
. - 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();
. - 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 { ... }
.
Object | Class |
---|---|
Instance of a class | Blueprint or template |
Represents a real-world entity | Defines the data and behavior |
Contains state and behavior | Contains instance variables, methods, and constructors |
- 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
, anddefault
. 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;
. - 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 { ... }
. - 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;
andpublic int getMyVariable() { return myVariable; }
. - 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.
- 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 theinterface
keyword.
ADVANTAGES AND DRAWBACKS
Advantages:
- Reusability of code
- Improved code maintainability
- Improved security
- Improved modularity
- Improved abstraction
Drawbacks:
- Overhead of creating and managing objects
- Increased complexity
SAMPLE EXERCISE WITH JAVA OOP
Novatesting would like to give you the following sample exercise so that you can get deeper into Java OOP knowledge.
Exercise:
- Create a class
Person
with private fieldsfirstName
,lastName
, andage
. - Implement getter and setter methods for each field.
- Create a subclass
Student
that extends thePerson
class. - Add a private field
studentId
to theStudent
class and implement its getter and setter methods. - 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”. - Create a class
Main
with amain
method. - In the
main
method, create an instance of theStudent
class and set its properties using the setter methods. - Print the information of the student using the
toString
method.
Answer to the Exercise:
- The
Person
class:
typescript
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;
}
@Override
public String toString() {
return "Name: " + firstName + " " + lastName + ", Age: " + age;
}
}
- The
Student
class:
java
public class Student extends Person {
private int studentId;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
@Override
public String toString() {
return "Name: " + getFirstName() + " " + getLastName() + ", Age: " + getAge() + ", Student ID: " + studentId;
}
}
- The
Main
class:
typescript
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.setFirstName("John");
student.setLastName("Doe");
student.setAge(22);
student.setStudentId(123456);
System.out.println(student.toString());
}
}
Output:
yaml
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.