Understand Object Oriented programing using java

OOPS Concepts

- Advertisement -

In this tutorial, I will explain the OOPS Core concepts in Java. OOPS, the concept is very mandatory for all developers, without having an idea about OOPS you did not able to design & develop object-oriented programming. Before starting this the article strongly you have an idea about what is OOPS why we have used in the core projects.

What is OOPS?
OOPS (Object Oriented Programming) mostly used for high-level modern programming languages like Java, PHP. The object is a very important module to design java application, OOPS allows to create of the object and manipulate from methods.

OOPS, Advantages:
1. Clear Structure, easy to understand.
2. Easy to find the program object explanation.
3. Objects names are classified.
4. Reused the Codes.
5. Saves developers time to write extra code.

OOPS Core Concept:
Below I will list the OOPS core concept and explain the concepts with an example. Here I will point out 7 OOPS concept but the main OOPS concepts
are,
1. Inheritance
2. Abstraction
3. Encapsulation
4. Polymorphism

Basic OOPS Concepts:
They are,

1. Inheritance
Inheritance one of the best feature in object-oriented programming. When creating new classes that covered from some of the attributes in the existing classes. It’s creating a parent-child relationship between the two classes.

Example:

Class Staff {
String designation = “Staff”;
String department = “MCA”;

void does() {

System.out.println(“Progressing”);

}
}
public class MathsStaff extends Staff {
String mainSubject = “Maths”;
public static void main (String args[]) {
MathsStaff obj = new MathsStaff();
System.out.println(obj.department);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
Obj.does();
}
}

Output

MCA
Staff
Maths
Progressing

 

2. Abstraction
When you have use abstraction, it avoids the same code multiple times. It represents things like Objects, Classes, and Variables.

Advantages of Abstraction:
1. Reduce the complexity of viewing the codes.
2. Avoid code duplication.
3. Increase the code Reusability.
4. Important details only provide.

Example:

- Advertisement -

abstract class Car {
//abstract method
public abstract void brand();
}
public class Baleno extends Car {
public void brand() {
System.out.println(“Nexa”);

}
public static void main(String args[]) {
Car obj = new Baleno();
obj.brand();
}
}

Output

Nexa

 

3. Encapsulation

Variable of Classes are always hidden from other classes. It’s providing the fields within a class private then access to them via public class
methods. So we can Re-use the object from our project & developing time also saved.

Advantages of Encapsulation:
1. Helps to binding the Data. (Instance Variable)
2. Useful for hiding the Data.
3. Flexible Code Design, so easy to maintain.

Example:

public class DemoEncapsulation
public static void main (String [] args) {
Encapsulate obj = new Encapsulate();
// Set Values for Variables
obj.setName (“John”);
obj.setDesignation (“Tester”);
obj.setSalary (150000);
//Display Values for Variables
System.out.println (“Name:” + obj.getName());
System.out.println (“Designation:” + obj.getDesignation());
System.out.println (“Salary:” + obj.getSalary());
}
}

Output:

Name: John
Designation: Tester
Salary: 150000

 

4 .Polymorphism
Polymorphism act, where object behaves differently in different ways. There are two types of polymorphism that’s like Compile time & Run time. In these two types achieved method overloading and method overriding.

Polymorphism Advantages:
1. Define multiple Constructors.
2. Reuse the existing Classes.
3. Without the need of Re-compilation.

Example:

Interface Car {
void speed (int speed);

}
class Toyota implements Car {
public void price (int price) {
System.out.println(“Toyota Price is:” + price);
}
}
class Maruti implements Car {
public void price (int price) {
System.out.println(“Maruti Price is:” + price);
}
}
public class PolymorphismDemo {
public static void main (String [] args) {
Car car;
car = new Toyota ();

car.price (1200000);
car = new Maruti();
car.price (800000);
}
}

Output

Toyota Price is 1200000
Maruti Price is 800000

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More