Object oriented programming with Java

Object-oriented programming with Java

Object oriented programming with Java

OOPs (Object-oriented programming) is a programming paradigm based on the concept of objects, which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

Class: Class is A template that specifies the state (property or field) and behavior (function Or method) that objects of its type will support. Refer below to a very simple class definition a

public class Test{
	int number = 10;
	public void print() {

Object: During run-time, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object of that class. That object will have its state, and access to all of the behaviors defined by its class. testObj is an Object of class Test.

Test testObj = new Test();

Abstraction: Objects in object-oriented programming language provide an abstraction that hides the internal implementation details. You just need to know which methods of the object are available to call and which input parameters are needed to trigger a specific operation.

In simple words, you don’t need to understand how a method is implemented inside a class and which kinds of actions it has to perform to create the expected result. Abstraction always refers to the implementation of a class. In Java, we can achieve abstraction by declaring a class abstract or using the interface.

Inheritance: It is a concept to implement reusability. When you want to create a class and there is a class that already has the code you want, here you can drive your new class from the existing class. In doing this you can reuse the behavior and state of that class. Read inheritance in detail.

Encapsulation: It is the concept of object-oriented programming that consists of minimizing the interdependence between classes and it is typically implemented through information hiding.

The beauty of encapsulation is the power of changing things without affecting its users.

In an object-oriented programming language like Java, you achieve encapsulation by hiding details using the accessibility modifiers (public, protected, private, plus no modifier which implies package-private). With these levels of accessibility, you control the level of encapsulation, the less restrictive the level, the more expensive change is when it happens and the more coupled the class is with other dependent classes (i.e. user classes, subclasses). Therefore, the goal is not to hide the data itself, but the implementation details on how this data is manipulated.

Encapsulation is a mechanism where you don’t restrict access to properties of a class but provide a contract on how others will use it. Encapsulation is not data hiding encapsulation leads to data hiding.

it is used to refer to one of two notations.

  • Restricting direct access to some of the object components.
  • Bundling of data with methods or functions operating on them.

Polymorphism: An Object can take more than one form. The most common use of polymorphism is when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A relationship can be considered as polymorphic.

There are two types of polymorphism:

  • Compile-time polymorphism
  • Run time polymorphism

Compile-time polymorphism is when an overridden method is called using a child class reference. In this case, during the compile time the compiler checks if the overridden method is present in there or not.

Run time polymorphism means when the call to an overridden method is resolved during run time rather than compile time. In this case, an overridden method is called through the reference variable of the base class. The determination of method call is based on the object referred to by the reference variable.

Let’s understand it with an example.

Create a class Animal.java

public class Animal {

	public void eat(){
		System.out.println("Animal eat");

Create a class Dog.java which extends Animal.java

public class Dog extends Animal{

	public void eat(){
		System.out.println("Dog eat");

create a class CompileTimeExample.java as shown below and run it.

public class CompileTimeExample {

	public static void main(String[] args) {
		Dog d = new Dog();

The output will be:

Dog eat

Key point: In this case, the method of Dog class will be called and the compiler will check if the eat method is present inside Dog class or not, which is not the case in run time polymorphism.

create a class RunTimeExample.java as shown below and run it.

public class RunTimeExample {

	public static void main(String[] args) {
		Animal a = new Dog();

The output will be:

Dog eat

Key Point: In this case, we have created a reference variable of Base class Animal but it is assigned to an object of child class dog. During compilation, the compiler will check if the eat method is Available inside the Animal class or not. However, during the execution, JVM call eat method of child (Dog) class. Here the call to an overridden method is defined during run time.

Recommended Read

Class-loader in Java



Happy Learning !!

Leave a Comment