OOPs Concept in Java

In this, we will learn the basic concept of OOPs (Object Oriented Programming).

In OOPs there are four pillars.

 1. Inheritance

 2. Abstraction

 3. Polymorphism

 4. Encapsulation

In oops there is the concept of object and class.

Object

It means a real-world entity such as a chair, table, etc. It can be physical or logical. The object is an instance of a class.

This is categorized by three features.

1. Identity.  Identity is a characteristic used to uniquely identify that object.

2. State. A Java object’s states are stored in fields that represent the individual characteristics of that object. It is represented by the attributes of an object.

3. Behavior. It is exposed through methods that operate its internal state.


Create an Object

In Java Object is created from the class. So to create an object we need to first create the class.

To create an Object of any class below is the syntax for it.

Syntax:

ClassName objectName = new ClassName()

For Example:

Java

public class MyClass {

    String className = "MyClass";

    public static void main(String[] args) {

        MyClass myObject=new MyClass();
       
        System.out.println(myObject.className);
    }
}


Class

The collection of objects is known as a class. It is a logical entity.

A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.

Class is a template of an object.

Create a Class

To create a class, use the keyword class

Note: Class first character should be uppercase. For example, MyClass, Employee.

Syntax:

public class ClassName{

}

For Example:

Java

public class MyClass {

    String className = "MyClass";
}


Inheritance

When one object acquires all the properties and behaviors of a parent class, it is known as inheritance. It is used to achieve runtime polymorphism.

Reusability. It provides code reusability, which means if some code already exists we can use it without writing the same logic again.

SuperClass. A class whose features are inherited is known as a superclass or parent class.

SubClass. The class that inherits the properties of another class is known as a Subclass.

For Example:

Create one parent class and use extends keyword to inherit this into the child class.

Parent Class.

public class ParentClass {

    String parentClass= "ParentClass";
}


The extends keyword is used to inherit the parent class into the child class.

Child Class 

public class MyClass extends ParentClass {

    String childClass = "MyClass";
}


Abstraction.

Hiding the internal details and showing the functionality is known as abstraction.

In Java, we can achieve abstraction by creating a class as abstract and by creating 

the interface.


For Example.

Abstract Class

abstract public class MyClass {

    String className = "MyClass";

}


Interface

public interface MyClass {

    void display();
}


Encapsulation.

Wrapping data and code together into a single unit is known as encapsulation. We 

can achieve encapsulation by creating all variables as private and creating methods

as public.

Example.

public class Employee {

    private int id;;
    private int age;
    private String firstName;
    private String lastName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}


Polymorphism

Polymorphism means many forms. In other words, if one task is done in more than 

one way is known as polymorphism. In java we use methods overloading and 

method overriding. We can say polymorphism in java is of two types Overloading and Overriding.

Methods Overloading Example

public class MyClass {
   
    //method overloading example
    public int sum(int x, int y) {
        return (x + y);
    }

    //method overloading
    public int sum(int x, int y, int z) {
        return (x + y + z);
    }


    public static void main(String args[]) {
        MyClass myObj = new MyClass();
       
        System.out.println(myObj.sum(20, 56));
        System.out.println(myObj.sum(40, 80, 30));
    }
}


Methods Overriding Example

Create an interface with one method as shown below.

public interface MyClass {
   
    void display();
}


Use implements keywords to achieve overriding

public class MyClassImp implements MyClass {

    @Override
    public void display() {
        System.out.println("Example of overriding");

    }
}


No comments:

Post a Comment