Object Oriented Programming in Java

Shahbaj Ansari
7 min readJun 7, 2021

Object-Oriented Programming (OOP) refers to a type of programming in which programmers define the data type of a data structure and the type of operations that can be applied to the data structure.

OOPs Concepts — Table of Contents

What is a Class ?

What is an Object ?

Constructors in java

Object Oriented Programming Principles

  • Inheritance
  • Encapsulation
  • Abstraction
  • Polymorphism

1) What is a Class?

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. Class in Java determines how an object will behave and what the object will contain.

Syntax :

class <class_name>{  
field;
method;
}

2. What is an Object?

An object is an instance of a class , An object in OOPS is nothing but a self-contained component that consists of methods and properties to make a particular type of data useful. e.g., chair, bike, marker, pen, table, car, etc.

An object has three characteristics:

  • State: represents the data (value) of an object.
  • Behavior: represents the functionality of an object such as deposit, withdraw, etc.
  • Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.
object and class relationship

3. Constructors in Java

A constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object.

Every time an object is created using the new() keyword, at least one constructor is called.

Example :

class Student {   
.......

// A Constructor with no arguments
new Student() {}
.......
}

// Creating an object of the Student class
Student s1= new Student(); // calls Student() constructor.

Rules for writing Constructor:

  • Constructor(s) of a class must have the same name as the class name in which it resides.
  • A constructor in Java can not be abstract, final, static, and Synchronized.
  • Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.

Types of constructors

i) No-argument constructor: A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates default constructor (with no arguments) for the class. And if we write a constructor with arguments or no-arguments then the compiler does not create a default constructor.

ii) Parameterized Constructor: A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values, then use a parameterized constructor.

class Student{// data members of the class.
String name;
int roll_no;
/* constructor will initialize data members with the values of passed arguments while object of that class created. */Student(String name, int roll){
this.name = name;
this.roll_no = roll;
}
}
class Learning {public static void main (String[] args) {// calling parameterized constructor
Geek geek1 = new Geek("John",10);
System.out.println("Student Name :" + s1.name + " , and Roll No. :" + s1.roll_no);
}
}

Output :

Student Name : John , and Roll No. : 10

4. Object Oriented Programming Principles

There are four building blocks of object-oriented programming namely Inheritance, Encapsulation, Abstraction, and Polymorphism.

4.1) Inheritance

Inheritance is a concept where all properties of one class can be inherited by the other class. It helps to reuse the code and establish a relationship between different classes.

inheritance in java

In Java, there are two types of classes

1. Parent class ( Super or Base class)

2. Child class (Subclass or Derived class )

A class that inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.

The keyword used for inheritance is extends, and the syntax is

class child-class extends parent-class {  
//methods and fields
}

Inheritance is further classified into 4 types:

I)Single Inheritance :

In single inheritance, one class inherits the properties of another. It enables a derived class to inherit the properties and behavior from a single parent class.

single inheritance

Here, Class A is the parent class and Class B is your child class which inherits the properties and behavior of the parent class.

Syntax :

class A{  
//methods and fields
}
class B extends A{
//methods and fields
}

II)Multilevel Inheritance :

When a class is derived from a class that is also derived from another class, i.e. a class having more than one parent class but at different levels, such type of inheritance is called Multilevel Inheritance.

multilevel inheritance

Here A is the parent class for B and class B is the parent class for C. So in this case class C implicitly inherits the properties and methods of class A along with Class B.

class A{  
//methods and fields
}
class B extends A{
//methods and fields
}
class C extends B{
//methods and fields
}

III) Hierarchical Inheritance :

When a class has more than one child class (subclasses) or in other words, more than one child classes have the same parent class, then such kind of inheritance is known as hierarchical.

hierarchical inheritance

Here, Class B and C are the child classes that are inheriting from the parent class i.e Class A.

class A{  
//methods and fields
}
class B extends A{
//methods and fields
}
class C extends A{
//methods and fields
}

IV) Hybrid Inheritance :

Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. Since multiple inheritances are not supported in Java as it leads to ambiguity, so this type of inheritance can only be achieved through the use of the interfaces.

hybrid inheritance

4.2) Encapsulation

Encapsulation is a mechanism where we bind our data and code together as a single unit. It also means to hide our data in order to make it safe from any modification.

encapsulation in java

We can achieve encapsulation in Java by following methods

  • Declaring the variables of a class as private.
  • Providing public setter and getter methods to modify and view the values of the variables.
class StudentCount{
private int numOfStudents = 0;
public void setNoOfStudents (int count){
numOfStudents = count;
}
public int getNoOfStudents () {
return numOfStudents;
}
}
public class Learning {
public static void main(String args[]){
StudentCount std = new StudentCount ();
std.setNoOfStudents(45);
System.out.println("Number of Students : " + std.getNoOfStudents());
}
}

Output: Number of Students: 45

4.3) Abstraction

Abstraction is a process of hiding the implementation details and showing only functionality. There are two ways to achieve abstraction in java.

i) Abstract Class

ii) Interface

Abstract class: Abstract class in Java is declared using the ‘abstract’ keyword. It can have abstract and non-abstract methods. An abstract class is required to be extended and its abstract methods should be implemented on that extended class. An abstract class can have constructors and static methods. We can achieve (0 to 100%) abstraction using Abstract classes.

Interface: Interface in Java is a blueprint of a class. It has abstract methods and static constants. In an interface, each method is public and abstract. It does not contain any constructor. We can achieve 100% abstraction using interfaces.

4.4) Polymorphism :

Polymorphism is made up of two words, ‘poly’ means many and ‘morph’ means forms, So Polymorphism means the ability to take many forms. In the context of OOP, we can say that Polymorphism allows an interface or method to have multiple implementations.

There are two types of Polymorphism

i) Run-Time Polymorphism

ii) Compile-Time Polymorphism

--

--

Shahbaj Ansari

4th Year CSE student at Neotia Institute of Technology Management and Science | Roll No. : 14400117016