Object-Oriented Programming In Java

Oops In Java

Satyendra Kumar Gupta
8 min readMay 9, 2021

Object-oriented programming based on real word entities. Like example: laptop, marker, pen, mobile, fan etc. Object-oriented programming is a method of programming based on a hierarchy of classes, and well-defined and cooperative objects. Mainly four types feature of oops polymorphism, inheritance, abstraction, encapsulation.

Here first need to understand what is an object and classes.

What is an Object?

Object is a real world entities. which contains properties(state), behaviour and identities. Like example: pen, So here body, refill, colour is a properties(state) of pen, writing is a behaviour of pen and unique id is identities of pen. In programming language properties define as a data member, behaviour as a member function(method) and The identities of the object is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.

What is a Class?

Class is used to represent group of similar object. In other word you can say, A class is a blueprint or prototype of similar objects. Class defined the states and behaviour of a real-world object.

Syntax of class:
class Class_Name{
data member(fields);
member function(methods);
}

Example of Class:

public class Marker{
String body; // data member(properties or state or fields)
String colour; // data member(properties or state or fields)
void writing(){ //member function(method or behaviour)
---- //write your logic here
----
}
}

Features of Oops:

Features of Oops based on concept of real world of object. Now we see about principal of object in Java programming language.

1-Polymorphism

2- Inheritance

3-Abstraction

4-Encapsulation

Polymorphism:

In simple words, we can say polymorphism as the ability of a single task perform in more than one ways. So polymorphism means many forms. There are two types of polymorphism in java.

1. Compile-time polymorphism(method overloading)

2. Run-time polymorphism(method overriding)

In java polymorphism achieve via two ways by using method overloading and method overriding.
First to need understand here Compile-time polymorphism and Run-time polymorphism.

1. Compile-time polymorphism: When object is bounded within functionality(method) at compile time. This is know as Compile-time polymorphism. It is achieve by method overloading.

What is method overloading:

Overloading is a,when two or more methods in one class have the same method name but different parameters. Please see below example of compile-time polymorphism in java.

Example of compile-time polymorphism in java:

class Calculator{
int add(int a, int b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}
}
public class Sample
{
public static void main(String args[])
{
Calculator cal= new Calculator();
System.out.println(cal.add(10, 20));
System.out.println(cal.add(10, 20, 30));
}
}
Output:
30
60

Here can you see, the parameter of function(method) determine at compile time. So that is compile time polymorphism.

1. Run-time polymorphism: When object is bounded within functionality(method) at run time. This is know as Run-time polymorphism. It is achieve by method overriding.

What is method overriding:

Overriding is a, when two methods have the same method name and parameters. One of the methods is in the parent class, and the other is in the child class. Please see below example of run-time polymorphism in java.

Example of Run-time polymorphism in java:

class Calculator{
public void add(int a, int b){
int sum=a+b;
System.out.println(sum +" "+ "from Calculator Class...");
}
}
public class Sample extends Calculator{

public void add(int a, int b){
int sum=a+b;
System.out.println(sum+" "+ "from Sample Class...");
}
public static void main(String args[]){
Calculator rfSample = new Sample();
rfSample.add(10,20);

Calculator cal = new Calculator();
cal.add(20,30);
}
}
Output:
30 from Sample Class...
60 from Calculator Class...

Here can you see, the parameter of function(method) determine at run time(when object is create then parameter assign ). So that is run time polymorphism.

Inheritance:

Inheritance means when one entity inherit all the properties and behaviours of a super entity that is knows as Inheritance.

In java When one object acquires all the properties and behaviours of a parent object. In real life if you want to access existing properties and behaviours then inheritance came in picture.

For Example:- If you use 2G or 3G sim network of Airtel or Vodafone in your 2G or 3G support phone. Also that sim use in your 4G support phone. That means you access(inherit) the behaviours of 2G and 3G sim in your 4G supported phone.

Then you can say in technical world, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Reason of use Inheritance in java:

1-Achieved Run-Time Polymorphism.

2-For Code Re-usability.

Syntax of Inheritance:

class class-Name extends SuperClass-name{    // here write methods and fields}

Here, The extends keywords use to inherit all properties and behaviours from super-class(existing or parent class). That means your current class features increases.

Example of Inheritance:

class Base {

int x;
int y;

void show(){
System.out.println(x);
System.out.println(y);
}

}
public class Child extends Base{
void getValue(int a, int b){
x=a;
y=b;
}

public static void main(String args[]){
Child ch=new Child();
ch.getValue(10,20);
ch.show();
}
}

Output:
10
20

Here you can see, you extends all properties and behaviours of Base(parent) class in child class and you access through child class object.

Note 1:

All the data member(fields or variables) and member functions(methods) of Base class(parent) available to child class if they are not private.

Note 2:

Inheritance at run time.It’s not compile time.

Types of Inheritance in Java:

1.Single Inheritance:

Syntax of Single Inheritance:

class Base{
}
public class Child extends Base{
}

Example of Single Inheritance:

class Base {

int x;
int y;

void show(){
System.out.println(x);
System.out.println(y);
}

}
public class Child extends Base{
void getValue(int a, int b){
x=a;
y=b;
}

public static void main(String args[]){
Child ch=new Child();
ch.getValue(10,20);
ch.show();
}
}

Output:
10
20

2.*Multiple Inheritance(achieve via Interface , not a class)

One interface can extends more than one interface known as multiple inheritance.

Syntax of Multiple Inheritance:

interface_keyword Interface_Name1{
}interface_keyword Interface_Name2{
}interface_keyword Interface_Name3 extends Interface_Name1, Interface_Name2 {
}
public class Child implements Interface_Name3 {
}

Example of Multiple Inheritance:

interface My{	void show();
}
interface My1{ void display();
}
interface My2 extends My, My1{ void print();
}
public class Child implements My, My1 { public void print(){ }

public void show(){
System.out.println("My Interface");
}

public void display(){
System.out.println("My1 Interface");
}
public static void main(String args[]){
Child ch=new Child();
ch.show();
ch.display();
}
}
Output:
My Interface
My1 interface

3.Multilevel Inheritance:

Syntax of Multilevel Inheritance:

class A{
}
class Base extends A{
}
public class Child extends B{
}

Example of Multilevel Inheritance:

class A{
int x=50;
}
class Base extends A{
// int x=20;
}
public class Child extends Base{
//int x=10;
void show(){
System.out.println(x);
}
public static void main(String args[]){
Child ch=new Child();
ch.show();
}
}
Output:
50

4. Hierarchical inheritance:

Syntax of Hierarchical inheritance:

class A{
}
class Base extends A{
}
public class Child extends A{
}

Example of Hierarchical Inheritance:

class A{

int x=50;
}
class Base extends A{

//int x=20;
void show(){
System.out.println(x);
}
}
public class Child extends A{
//int x=10;
void show(){
System.out.println(x);
}

public static void main(String args[]){
Base base=new Base();
Child ch=new Child();
base.show();
ch.show();
}

}

Output:
50
50

5. Hybrid inheritance:

Syntax of Hybrid inheritance:

class A{
}
class B extends A{
}
class C extends A{
}public class D extends B{
}

Here before given example, I want to be explain what is hybrid inheritance:

A hybrid inheritance is a combination of more than one types of inheritance. For example when class B And C extends class A And another class D extends class B or Class C or both individual extends then this is a hybrid inheritance. Please see in example:

Example of Hybrid Inheritance:

class A{
public void show(){
System.out.println("Class A");
}
}
class B extends A{
public void show(){
System.out.println("Class B");

}
}
class C extends A{
public void show(){
System.out.println("Class C");
}
}public class D extends B{
public void show(){
System.out.println("Class D");
}
public static void main(String args[]){
D d=new D();
d.show();
}
}Output:
Class D

Abstraction:

Data abstraction is the process of hiding certain details and showing only essential information to the user. You can say in technically only show functionality and hiding complexity.

For example: switch on->fan moving, corruption, ATM machine, mobile phone calls etc.

Abstraction can be achieved with either abstract classes or interfaces .

* Abstraction is used to achieve to generalisation

* Abstraction is used to define standard

for example:

1. Bird →is the general Type

But may be flying bird or Non flying bird

2. Vehicle → is the general Type

But may be speed, Car, scooter, Motorcycle etc.

**Abstraction is pie feature of Oops.

Abstract Class:

It is a restricted class that means you can create object of that class. But if you want to access of this class features, then it must be inherited from another class. If you create abstract class it must be to use abstract keyword before class name. Then this class know as abstract class. An Abstract class can have both abstract and normal methods.

Abstract Method:

It can use only in abstract class, and only give declaration that means, it does not have definition(a body). It must be provide definition(implementation or a body) by the child(sub class) class.

Syntax of Abstract Class:

abstract_keyword class Class_name{}

Example of Abstract class:

abstract class HelloAbstract{
public abstract void display(); // abstract method
public void show(){
System.out.println("From HelloAbstract class...");
}
}
public class Hello extends HelloAbstract {

public void display(){
System.out.println("From Hello class...");
}
public static void main(String args[]){
Hello hello=new Hello();
hello.display();
hello.show();
}
}
Output:
From Hello class...
From helloAbstract class...

Some Rules of Abstract Class:

Rule 1: In Abstract class not necessary to contain abstract methods.

Rule 2: We must override all the abstract method in child class.

Rule 3: We never call abstract method explicitly.

Some Points In Abstract class:

1. Can we have constractor in abstract class — Yes.

2.Can Be declare an abstract method as a static — No.

Interface:

If one entity access from the another entity is called Interface.

For example: you access TV from your remote, you access your data base using java API (like JDBC).

Interface is the also knows blue print of class. Using interface to achieve fully abstraction. And also interface is use to define standards.

  • Interface is by default abstract.

Syntax of Interface:

interface_keyword Interface_name{

}

Example of Interface:

interface My{
void show();
}
public class Child implements My{
public void show(){
System.out.println("Hello child....");
}
public static void main(String args[]){
Child ch=new Child();
ch.show();
}
}
Output:
Hello child....

Some Rules of interface:

Rule 1: One class can implements one Interface

Rule 2: One class can implements more than one interface

Rule 3: One interface can extends other interface

Rule 4: One interface can extends more one interface.

Encapsulation:

Encapsulation means binding object data(state or fields) and behaviour(function or method) into single unit. Like if you create class, if you want to that class behave as encapsulated the declare all data member as a private. If data member is private it means, It can only be accessed within the same class. And more one things No outer class can access private data member of other class.

Example of Encapsulation:

enclose car engine, enclose ATM machine, capsule(medicine) etc.

Syntax of Encapsulation Class:

class Class_name{
data member(fields) // as a private
}

Example of Encapsulation Class:

class Employee{

private int empId;
private String empName;
//Create here getter and setter methods public int getEmpId(){
return empId;
}
public String getEmpName(){
return empName;
}
public void setEmpId(int id){
empId=id;
}
public void setEmpName(String name){
empName=name;
}
}
public class TestEncupsulation{
public static void main(String args[]){
Employee emp=new Employee();
emp.setEmpId(123);
emp.setEmpName("XYZ");
System.out.println("Employee Id: " +emp.getEmpId());
System.out.println("Employee Name: " +emp.getEmpName());
}
}
Output:
Employee Id: 123
Employee Name: XYZ

Conclusion:

Now a days, almost every programming language based on Oops concept.Because Oops concept based on real word entity means object. And also Oops concept follow Bottom-Up approach which that provide flexibility to write code. And also other features like code re-usability, code maintainability and also provide run time bonding like that others programming approach.

Thanks.

--

--