Sitemap

Object-Oriented Programming (OOP) Concepts in Java

4 min readJul 1, 2023

Introduction:
Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects. It allows developers to create reusable and modular code by organizing data and functionality into objects. Java is a widely-used programming language that supports OOP principles. In this article, we will explore the core concepts of OOP in Java,
along with example codes to illustrate each concept.

Classes and Objects:
In Java, classes are blueprints for creating objects. They define the attributes (data) and behaviors (methods) that objects of the class will possess. Objects are instances of classes. Let’s take a simple example of a class called “Lamp” to understand this concept:

class Lamp {

// stores the value for light
// true if light is on
// false if light is off
boolean isOn;
String lightcolor;

// method to turn on the light
void turnOn() {
isOn = true;
System.out.println("Light on? " + isOn);

}

// method to turnoff the light
void turnOff() {
isOn = false;
System.out.println("Light on? " + isOn);
}
}


In the above code, we have defined a class called “Lamp” with a one attribute: lightcolor. It also has a methods called turnoff() and turnOn(), which simply prints a message. We can create objects of this class as follows:

// create objects led and halogen
Lamp led = new Lamp();
Lamp halogen = new Lamp();
// turn on the light by
// calling method turnOn()
led.turnOn();
led.lightcolor="yellow"
// turn off the light by
// calling method turnOff()
halogen.turnOff();
halogen.lightcolor="white"

Here, we create objects led and halogen of the class lamp and set its attributes. We then invoke the turnOff() and turnOn() methods of the led and halogen objects.

Encapsulation:
Encapsulation refers to the bundling of data and methods within a class, hiding the internal details from the outside world.
This principle promotes data integrity and provides control over how data is accessed. Consider the following example:

class EncapsulationDemo{
private Strind company_name;
private String employee_name;
private int employee_age;

//Getter and Setter methods
public String getCompName(){
return company_name;
}

public String getEmpName(){
return employee_name;
}

public int getEmpAge(){
return employee_age;
}

public void setEmpAge(int newValue){
employee_age = newValue;
}

public void setEmpName(String newValue){
employee_name = newValue;
}

public void setCompName(String newValue){
company_name= newValue;
}
}
public class EncapsTest{
public static void main(String args[]){
EncapsulationDemo obj = new EncapsulationDemo();
obj.setEmpName("vinuka");
obj.setEmpAge(24);
obj.setCompName("Software Company");
System.out.println("Employee Name: " + obj.getEmpName());
System.out.println("Employee Company: " + obj.getCompName());
System.out.println("Employee Age: " + obj.getEmpAge());
}
}

In above example all the three data members (or data fields) are private, which cannot be accessed directly. These fields can be accessed via public methods only. Fields employee_name , employee_age and company_name are made hidden data fields using encapsulation technique of OOPs.

Inheritance:

Inheritance in java is a concept that acquires the properties from one class to other classes; for example, the relationship between father and son. Inheritance in Java is a process of acquiring all the behaviours of a parent object.

Class PetAnimal {

// field and method of the parent class
String name;
public void eat() {
System.out.println("I can eat");
}
}

// inherit from PetAnimal
class Dog extends PetAnimal {

// new method in subclass
public void display() {
System.out.println("My name is " + name);
}
}

class Main {
public static void main(String[] args) {

// create an object of the subclass
Dog labrador = new Dog();

// access field of superclass
labrador.name = "Rohu";
labrador.display();

// call method of superclass
// using object of subclass
labrador.eat();

}
}

Derived Class/Sub-class: ​Derived class is a class that inherits from a base class. It is also known as subclass or child class.

Get Vinuka kodithuwakku’s stories in your inbox

Join Medium for free to get updates from this writer.

Base Class/Superclass: ​The base class is the main class where derived classes inherit the features. It is also known as the superclass or parent class.

Abstraction:

Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don’t know the internal processing about the message delivery.

// Java Program to implement
// Java Abstraction

// Abstract Class declared
abstract class Animal {
private String name;

public Animal(String name) { this.name = name; }

public abstract void makeSound();//this is a aabstract method

public String getName() { return name; }
}

// Abstracted class
class Dog extends Animal {
public Dog(String name) { super(name); }

public void makeSound()
{
System.out.println(getName() + " barks");
}
}

// Abstracted class
class Cat extends Animal {
public Cat(String name) { super(name); }

public void makeSound()
{
System.out.println(getName() + " meows");
}
}

// Driver Class
public class AbstractionExample {
// Main Function
public static void main(String[] args)
{
Animal myDog = new Dog("Buddy");
Animal myCat = new Cat("Fluffy");

myDog.makeSound();
myCat.makeSound();
}
}

Pholymorphism:

Pholymorphism is one of the OOP feature that allows us to perform a single action in different ways. There are 2 type of Pholymorphism. Those are

  1. Runtime Polymorphism
public class Animal{
public void sound(){
System.out.println("Animal is making a sound");
}
}

class Horse extends Animal{
@Override
public void sound(){
System.out.println("Neigh");
}
public static void main(String args[]){
Animal obj = new Horse();
obj.sound();
}
}

2.compile time polymorphism

class Overload
{
void demo (int a)
{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class MethodOverloading
{
public static void main (String args [])
{
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
}
}