Object-Oriented Programming system in Java

Kaushik Shaw
6 min readMay 10, 2021

--

There are several programming language which are available in today’s programming world. Every programming language has their own pros and cons so today I am here to discuss about java(OOPs concept). So before starting we have to know why this concept is introduced? So the reason, it related to the real world entities .The main aim of oops is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

Simula is the first fully object oriented programing language which is introduced in 1967.There are 4 paradigms in oops

  1. Polymorphism
  2. Abstraction
  3. Inheritance
  4. Encapsulation

But before starting with this we have to know about Class, Object, Method.

Object:- It is a instance of a Class. We are fully surrounded with object like building,phone etc. To build this kind of object we need a blueprint.This blueprint is known as class.

Class:- It is user define prototype.It’s helps to build the object. It provides initial values for state(member variable attributes) and implementations of behavior(member function or methods). The user-defined objects are created using class keyword.It defines a nature of a future object.

Method:- In the class we define methods which contains a set of code to perform some amount of work.

By an example we can understand that car is a class and green ,red and blue are objects and the properties/behavior of all of this objects are methods.

OOPs features

1.Polymorphism:- The word polymorphism is made up of two words ‘poly’ means many and ‘morphs’ means forms. Example water has many forms like at certain temperature it is ice after for certain temperature it has water and after certain temperature it is vapour.

So there are two way we can achieve polymorphism in java

A.Compile Time Polymorphism:-

It is also known as method overloading. In this technique multiple methods are written in same class by changing the type of argument , number of arguments and sequence of arguments.why it is called compile time polymorphism is because the decision of which method is to be called is made at compile time.

public class Calc{
public int sum(int a, int b){
return a + b;
}
public double sum(int a, double b){ //overloading method
return a + b;
}
public double sum(double a, int b){ //overloading metod
return a + b;
public double sum(double a, double b){ //overloading method
return a + b;
}
}

B. Run Time Polymorphism:-

It is also known as method overriding.In this technique where same methods are written in different classes with same type of arguments ,same number of arguments and sequence of arguments. why it is called run time polymorphism is because in the runtime, JVM figures out the object type and would run the method that belongs to that particular object.

class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog { public static void main(String args[]) {

Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}

Output

Animals can move
Dogs can walk and run

In the above example, you can see animal can move but dog which is under the animal have his own feature he can walk as well as run so Dog class inherit the animal class and add some more functionality .

2.Abstraction:- It is a process to show the functionality and hide the complexity because java is a secure language.we can achieve abstraction by two way in java .

A. Abstract class

B. Interface

By the help of Abstract class we can achieve( 0–100)% abstraction because it can have abstract method(no body) ,non abstract method(concrete method), constructor & static method. And we can not create the object of this abstract class because this classes are incomplete, if java allow to create object of this class their would be no actual implementation of method to invoke.To create the object of Abstract class we have to inheritance this class to another class then we can create the object .

But in case of Interface we can achieve 100% abstraction till java 1.7 in java 1.8 we can define concrete method in Interface by using default or static keyword.In Interface no access specifiers are use.By default all are public.

So both have some advantages like Interface has more accessibility then abstract class. It is consume less memory and the degree of freedom is more and also we can achieve multiple inheritance by Interface.An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it up to java 1.7 .

3.Inheritance:- By the help of inheritance we inherit the parent class properties into child class by the help of extends keyword. Also we can inherit interface into class by the help of implements keyword. There are 5 types of inheritance in java

Those are

A.Single level inheritance:- In which child class inherit one parent class.

B. Multi Level Inheritance:- In which one child class inherit to his parent class and this class is also inherit to his parent class an goes on. for multi level inheritance we need at least 3 classes.

As you can see in the above diagram, class A is base class of class B (derived class), class B is the base class of class C (derived class).

C. Hierarchical Inheritance:-

As you can see in the above diagram, class A is base class of class B ,C and D. Class B,C and D called derived class.

D. Hybrid Inheritance:-


c
|
---------------
↑ ↑
| |
A B

|
D

As you can see in the above diagram, class C is base class of class A(derived class) and B(derived Class). class A is the base class of class D(derived class).

E. Multiple Inheritance:- Java can’t support multiple inheritance with the help of classes due to ambiguity error . But we can achieve multiple inheritance in java with the help of interface.

In this above picture A and B are the interface C is the class which implements both the interfaces.

4.Encapsulation:-It is a mechanism of wrapping the data (variables) by using the private keyword and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes and the properties of this classes are hidden by the use of private keyword , to access this properties we are using some getter setter methods. Therefore, it is also known as data hiding.

Example

Following is an example that demonstrates how to achieve Encapsulation in Java −


public class EncapTest {
private String name;
private String idNum;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public void setAge( int newAge) {
age = newAge;
}
public void setName(String newName) {
name = newName;
}
public void setIdNum( String newId) {
idNum = newId;
}
}

Conclusion

OOPs is particularly helpful when problems need some real-world modeling as well as when the solution needs the Bottom-Up approach.

--

--