Use of OOP in Java
Classes and Objects
Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
A Class is like an object constructor, or a "blueprint" for creating objects.
class Car { // class definition
int modelYear;
String modelName;
}
An object is created from a class. We can create many objects from a class. An object is also called an instance of a class.
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // create an object of Car
myCar.modelYear = 1969; // set the value of the modelYear attribute to 1969
myCar.modelName = "Mustang"; // set the value of the modelName attribute to Mustang
System.out.println(myCar.modelYear + " " + myCar.modelName); // output the value of the modelYear and modelName attributes
}
}
PC $ java Main
1969 Mustang
Encapsulation
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
There are 3 access modifiers in Java:
- private: The code is only accessible within the declared class.
- public: The code is accessible from all classes.
- protected: The code is accessible in the same package and subclasses.
Private Variables
class Person {
private String name; // private variable
}
class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.name = "John"; // error
System.out.println(myObj.name); // error
}
}
PC $ java Main
Main.java:6: error: name has private access in Person
myObj.name = "John"; // error
^
Main.java:7: error: name has private access in Person
System.out.println(myObj.name); // error
^
2 errors
Did you get an error? Because you cannot access a private variable from outside the class.
Public Variables
class Person {
private String name; // private variable
public String getName() { // getter method
return name;
}
public void setName(String newName) { // setter method
this.name = newName;
}
}
class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("John");
System.out.println(myObj.getName());
}
}
PC $ java Main
John
Protected Variables
class Person {
protected String name; // protected variable
}
class Student extends Person {
private int graduationYear;
public static void main(String[] args) {
Student myObj = new Student();
myObj.graduationYear = 2010;
myObj.name = "John";
System.out.println("Name: " + myObj.name);
System.out.println("Graduation Year: " + myObj.graduationYear);
}
}
class Main {
public static void main(String[] args) {
Student myObj = new Student();
myObj.graduationYear = 2010;
myObj.name = "John";
System.out.println("Name: " + myObj.name);
System.out.println("Graduation Year: " + myObj.graduationYear);
}
}
PC $ java Main
Name: John
Graduation Year: 2010
To achieve encapsulation in Java:
- Declare the variables of a class as private.
- Provide public setter and getter methods to modify and view the variables values.
class Person {
private String name; // private variable
public String getName() { // getter method
return name;
}
public void setName(String newName) { // setter method
this.name = newName;
}
}
class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("John");
System.out.println(myObj.getName());
}
}
PC $ java Main
John
Inheritance
Inheritance is a mechanism in which one class acquires the properties and behavior of another class. The class that inherits the properties of another class is known as a subclass (derived class), and the class whose properties are inherited is known as a superclass (base class).
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.bark();
d.eat();
}
}
PC $ java Main
barking...
eating...
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends
Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends
Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
myAnimal.sound();
myDog.sound();
myCat.sound();
}
}
PC $ java Main
Animal makes a sound
Dog barks
Cat meows
Abstraction
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
abstract class Animal {
abstract void sound();
}
class Dog extends
Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound();
}
}
PC $ java Main
Dog barks
Association
Association is a relationship between two classes. It describes how objects are related to each other and how they interact with each other.
class Car {
private String model;
public Car(String model) {
this.model = model;
}
public String getModel() {
return model;
}
}
class Person {
private String name;
private Car car;
public Person(String name, Car car) {
this.name = name;
this.car = car;
}
public String getName() {
return name;
}
public Car getCar() {
return car;
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Mustang");
Person myPerson = new Person("John", myCar);
System.out.println(myPerson.getName() + " has a " + myPerson.getCar().getModel());
}
}
PC $ java Main
John has a Mustang
Aggregation
Aggregation is a special form of association where an object is composed of one or more other objects. It represents a "has-a" relationship between objects.
class Address {
String city, state, country;
public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
}
class Employee {
int id;
String name;
Address address;
public Employee(int id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
void display() {
System.out.println(id + " " + name);
System.out.println(address.city + " " + address.state + " " + address.country);
}
}
public class Main {
public static void main(String[] args) {
Address address1 = new Address("Gurgaon", "Haryana", "India");
Address address2 = new Address("Delhi", "Delhi", "India");
Employee e = new Employee(101, "John", address1);
Employee e2 = new Employee(102, "David", address2);
e.display();
e2.display();
}
}
PC $ java Main
101 John
Gurgaon Haryana India
102 David
Delhi Delhi India
Composition
Composition is a stronger form of aggregation where an object is composed of one or more other objects, and the composed objects cannot exist independently of the container object. It represents a "part-of" relationship between objects.
class Address {
String city, state, country;
public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
}
class Employee {
int id;
String name;
Address address;
public Employee(int id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
void display() {
System.out.println(id + " " + name);
System.out.println(address.city + " " + address.state + " " + address.country);
}
}
public class Main {
public static void main(String[] args) {
Address address = new Address("Gurgaon", "Haryana", "India");
Employee e = new Employee(101, "John", address);
e.display();
}
}
PC $ java Main
101 John
Gurgaon Haryana India
Dependency
Dependency is a relationship between two classes where one class depends on another class. It occurs when a class uses another class as a parameter or local variable.
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
Engine engine; // Car "has-a" Engine
Car(Engine engine) {
this.engine = engine;
}
void start() {
engine.start();
System.out.println("Car started");
}
}
public class Main {
public static void main(String[] args) {
Engine engine = new Engine();
Car car = new Car(engine);
car.start();
}
}
PC $ java Main
Engine started
Car started
These are some of the key concepts of Object-Oriented Programming (OOP) that are used in Java to design and develop software applications. By utilizing these concepts, developers can create modular, extensible, and maintainable software applications that are easy to understand, modify, and scale.