Android developmentAndroid tutorial

OOPS Concepts in Java – OOPS Concepts Example

OOPS Concepts in Java – OOPS Concepts Example
713views

OOPS Concepts in Java, Programming is made easier by the Object-Oriented Programming Concepts. You will have difficulty designing systems using the object-oriented programming model if you don’t know what OOPS concepts are.

What is an Object-Oriented Programming Model (OOP)?

The concept of objects is the core of object-oriented programming.

What’s an Object?

An instance of a class is called an object. It can contain properties and functions. These functions are similar to real-world objects. For example, your car, house, laptop, etc. All objects are objects. They can perform certain actions by having specific properties.

What’s a Class?

The blueprint for objects is defined by the Class. They determine the functionalities and properties of objects. Laptop, for example, is a class. Your laptop is an instance.

 

OOPS concepts are:

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance
  5. Association
  6. Aggregation

Let’s take a closer look at each object-oriented programming concept. For code examples, we will use Java programming language to show you how to implement OOPS concepts using Java.

1. Abstraction

Abstraction refers to the idea of hiding internal details and explaining things in simple terms. A method that adds two integers is an example. The method’s internal processing is hidden from the outside world. In object-oriented programming, there are several ways to achieve abstraction.

An excellent example of abstraction is a Java program. Java converts simple statements into machine language, and hides the inner details of the implementation from the outside world.

2. Encapsulation

Encapsulation is a technique that allows you to implement abstraction in object-oriented programing. Encapsulation can be used to restrict access to methods and class members.

Access modifier keywords can be used to encapsulate in object-oriented programming. Encapsulation in Java can be achieved, for example, using protected, and publicly keywords.

3. Polymorphism

Polymorphism refers to the phenomenon where objects behave differently in different situations. There are two types: compile-time polymorphism or runtime polymorphism.

overloading achieves compile-time polymorphism. You can create a class like the one below OOPS Concepts Example.

 

public class Circle {

    public void draw(){
        System.out.println("Drwaing circle with default color Black and diameter 1 cm.");
    }

    public void draw(int diameter){
        System.out.println("Drwaing circle with default color Black and diameter"+diameter+" cm.");
    }

    public void draw(int diameter, String color){
        System.out.println("Drwaing circle with color"+color+" and diameter"+diameter+" cm.");
    }
}

 

We have multiple draw methods, but each one has a different behavior. Because all methods have the same name and each argument is different, this is called method overloading. Compile-time polymorphism is when the compiler can identify which method to invoke at compile time.

When there is an “IS-A relationship between objects, runtime polymorphism can be implemented. This is also known as a method-overriding, because the subclass must override the superclass method to runtime polymorphism.

If we work in terms of the superclass the actual implementation class will be determined at runtime. The compiler cannot decide which class method is to be invoked. This decision is made at runtime OOPS Concepts Example.

 

package com.codeplayon.test;

public interface Shape {

    public void draw();
}


package com.codeplayon.test;

public class Circle implements Shape{

    @Override
    public void draw(){
        System.out.println("Drwaing circle");
    }

}


package com.codeplayon.test;

public class Square implements Shape {

    @Override
    public void draw() {
        System.out.println("Drawing Square");
    }

}

 

Shape is the superclass and there are two subclasses Circle and Square. Below is an example of runtime polymorphism.

   Shape sh = new Circle();
    sh.draw();
    Shape sh1 = getShape(); //some third party logic to determine shape
    sh1.draw();

 

4. Inheritance

Inheritance refers to an object-oriented programming idea where an object is built upon another object. Inheritance refers to the mechanism of code reuse. The object being inherited is the superclass, and the subclass that inherits it is the subclass.

To implement inheritance, we use extends keyword. Here is an example of inheritance in Java OOPS Concepts Example.

package com.journaldev.java.examples1;

class SuperClassA {

    public void foo(){
        System.out.println("SuperClassA");
    }

}

class SubClassB extends SuperClassA{

    public void bar(){
        System.out.println("SubClassB");
    }

}

public class Test {
    public static void main(String args[]){
        SubClassB a = new SubClassB();

        a.foo();
        a.bar();
    }
}

 

5. Association

The OOPS concept of association is used to describe the relationship between objects. The association is the way to define the multiplicity of objects. As an example, Teacher objects and Student objects. A teacher and student can have a one-to many relationship. A student may also have a one to many relationship with teacher objects. Both teacher objects and students are independent.

6.Aggregation

OOPS Concepts Example Aggregation is a unique type of association. Aggregation is a special type of association where objects have their own lives, but ownership exists. If there is a “HAS-A” relationship between ownership and objects, then it’s an example of aggregation.

 

Read More Tutorial