OOP in Java Programming using programming example


Java is known as pure object oriented programming language. How java implements its oop and how it helps in programming to a problem. Let us look at it using programming example.

class Bike{
    public int speed;
    
    //constructor 1
    Bike(){
        System.out.println("the parameterless constructor for Bike Class");
    }
    
    //constructor 2(constructor overloading - Polymorphism)
    Bike(int a){
        System.out.println("the parametrized constructor for Bike Class and parameter passed is:"+a);
    }
    
    //making method (encapsulation)
    public void setSpeed(int speed){
    }
}

class MountainBike extends Bike{
//MountainBike extending Bike class (Inheritance)
    
    @override
    pulic void setSpeed(int speed){ //method overriding (code reuse)
        super.speed = speed;
    }
    
    public static void main(String[] args){ //main method (one entry point for application)
        Bike avon = new MountainBike(); // creating object (OOP class-object scenario)
        avon.setSpeed(10); //set speed using method, and hide the data's direct access (abstraction)
    }
}


In the programming example above there are two sample classes and I have taken the example of the Bikes. We can define the bikes in various categories like mountain bike or racing bike or simple bike. All these have most of the feature common like the speed, number of wheels, frame structure. We can define these property in a Class.

In the program above the Bike class define the blueprint of Bike, thought it could be complex I am using least and shortest possible example for this scene to explain.

Polymorphism: There are two constructor for this class, If you see only difference is in the number of parameter. So the constructor in Bike class  has two forms and so it is what I meant by polymorphism.

Encapsulation: The encapsulation is defined as the breaking down of code in small possible code structure so that it is separated from other part. In Bike example I have created a method and done nothing with it. Technically I could use it to define the speed of the Bike but as we don't know what type of Bike its going to be, I have left it undefined. So I am breaking code to serve a particular purpose, that is encapsulation.

Inheritance: The Class MountainBike is inheriting the property from the Bike class and hence its variable 'Speed' and its method 'setSpeed' too. here we don't need to write the code again. And even we are defining the 'setSpeed' method which was originally the method of base class 'super' class in Java. This is what inheritance is all about, the reduction and reuse of code.

One Entry Point: The one entry point serves the security purpose as well as reduce the confusion and management of the program. Basically the 'main' method is the Parent thread when we start the application all the access we make from there on are all child thread.

Class-Object: The OOP is all about defining the property and behaviour. Here we are defining the property and behaviour in the Class, and then we use the object to define a particular type which is using the property and behaviour.

Dynamic Binding: The dynamic binding is obtain in the code when we use the Parent Object type and the child object in the reference. This allows us to define dynamically what we want to refer.