Java OOP concepts

Java OOP concepts

Oops concept in programming

Object-Oriented Programming is a methodology to design a program using classes and objects. It simplifies software development and maintenance by incorporating certain principles.​

Abstraction

Data Abstraction is the property by which only the essential details are displayed to the user. The trivial or the non-essential units are not displayed to the user.​ Example shown below

Concerning an ATM, we as a consumer only interacts with the interface. All the decision-making happens on the backend. These decisions include checking, calculating and updating the balance.​

There are two ways to achieve abstraction in java

  1. Abstract class

  2. Interface

Example of an Abstract class that has an abstract method

In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is provided by the Honda class.

abstract class Bike{

abstract void run();

}

class Honda4 extends Bike{

void run(){

System.out.println("running safely");

}

public static void main(String args[]){

Bike obj = new Honda4();

obj.run();

}

}

Interface in Java

An interface in Java is a blueprint of a class. It has static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in java.

Java Interface Example

In this example, the Printable interface has only one method, and its implementation is provided in the A6 class.

interface printable{

void print();

}

class A6 implements printable{

public void print(){

System.out.println("Hello");

}

public static void main(String args[]){

A6 obj = new A6();

obj.print();

}

}

Inheritance

In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:​

superclass (parent) - the class being inherited from​ uses the extends keyword.​

​subclass (child) - the class that inherits from another class​

Types Of Inheritance

Single Inheritance Example

When a class inherits another class, it is known as a single inheritance. In the example given below, the Dog class inherits the Animal class, so there is a single inheritance.

class Animal{

void eat(){System.out.println("eating...");}

}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

}

class TestInheritance{

public static void main(String args[]){

Dog d=new Dog();

d.bark();

d.eat();

}}

Multilevel Inheritance Example

When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.

class Animal{

void eat(){System.out.println("eating...");}

}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

}

class BabyDog extends Dog{

void weep(){System.out.println("weeping...");}

}

class TestInheritance2{

public static void main(String args[]){

BabyDog d=new BabyDog();

d.weep();

d.bark();

d.eat();

}}

Hierarchical Inheritance Example

When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.

class Animal{

void eat(){System.out.println("eating...");}

}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

}

class Cat extends Animal{

void meow(){System.out.println("meowing...");}

}

class TestInheritance3{

public static void main(String args[]){

Cat c=new Cat();

c.meow();

c.eat();

}}

Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from the child class object, there will be ambiguity to call the method of the A or B class.

Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.

Polymorphism

The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. ​

Example A person at the same time can have different roles. Like a man at the same time is a father, a husband and an employee. So, the same person possesses different behavior in different situations.​

Encapsulation

  • Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds code and data together.​

  • Another way to think about encapsulation is, that it is a protective shield that prevents the data from being accessed by the code outside this shield. ​

Why Encapsulation?

  • Better control of class attributes and methods

  • Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)

  • Flexible: the programmer can change one part of the code without affecting other parts

  • Increased security of data

Let's look at an example below

The get method returns the variable value and the set method sets the value.

The syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case:

Example

public class Person {
  private String name; // private = restricted access

  // Getter
  public String getName() {
    return name;
  }

  // Setter
  public void setName(String newName) {
    this.name = newName;
  }
}

Example explained

The get method returns the value of the variable name.

The set method takes a parameter (newName) and assigns it to the name variable. The this keyword is used to refer to the current object.

However, as the name variable is declared as private, we cannot access it from outside this class:

Example

public class Main {
  public static void main(String[] args) {
    Person myObj = new Person();
    myObj.name = "John";  // error
    System.out.println(myObj.name); // error 
  }
}

we use the getName() and setName() methods to access and update the variable.