Software Training Institute in Chennai with 100% Placements – SLA Institute

Easy way to IT Job

Share on your Social Media

Java OOPs Concepts: What is, Basics with Examples

Published On: March 21, 2023

Java OOPs Concepts: Basic with Examples

Java programming language is built based on OOPs (Object-Oriented Programming) for improving code readability and reusability. OOPs concepts in Java eliminate the flaw of conventional programming to structure a program with  several new features and some of the OOPs-based programming languages are Java, C++, and Pascal. Oops is an approach to modularize the program with a partitioned area for memory and create copies of modules for on-demand.

Advantages of OOPs concepts:

Oops concepts ties the data and protects it from unintentional modification from any other existing functions. The following are the advantages of Object-Oriented Programming Concepts that treat data as an element but not allow it to flow across the system freely.

Re-usability: The class in OOPs is reusable for functions several times to avoid building repetitive codes again and again.

Data Redundancy: It saves data storage for similar functionality codes and we can inherit the same class multiple times from the same storage.

Code Maintenance: It saves time for maintaining the codes as it is easy to call and modify the classes as per necessity.

Data Security: Through the abstract mechanism of OOPs, It hides the data to maintain security by keeping limitations to exposure and providing only necessary data to other classes.

Design Benefits: It helps the designers to make an extensive design to bring a better design with fewer flaws. It is easy to separate non-OOP blocks while reaching some limits.

Better Productivity: OOPs help the developer to have enhanced productivity by finishing the proper program, including in-built features, easy-to-read codes, easy-to-write codes, and easy maintenance.

Simple and Easy Troubleshooting: It helps the testers and developers troubleshoot the program that has some common issues like widget files, WhaleFlumper, and code flow.

Flexibility: It is possible with polymorphism as it provides the developer to use the code with flexibility by allowing them to use the data or method as per the requirement.

List of Object-Oriented Programming Concepts in Java

Following are the OOPs concepts in Java with examples

Objects and Classes

Objects are the basic units of OOPs that represent real-time entities. Objects can be created multiple times in the Java program and they will be interacting through invoking methods. An object includes state, behavior, and identity. The state represents the attributes and reflects properties of an object, behavior is represented by methods and reflects the response of an object to the request of other objects, and identity is the unique name to enable an object to interact with other objects.

Class is a user-defined prototype from which objects are created and it represents the set of methods that are general for all types of objects. A class contains components such as a modifier (public or default for access), the class keyword for creating a class, a class name that should begin with a capital letter, a superclass to act as a parent class, interfaces that are separated with comma, and body of class that surrounded by braces. There are nested classes, anonymous classes, and lambda classes used for real-time applications.

Examples of Objects and Classes

Creating a Class

public class Main {

int x = 5;

}

Creating an Object

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

   }

}

Abstraction

Abstraction is one of the most used Java OOPs Concepts that hides confidential information and shows necessary data to the users. It helps in avoiding repetitive code, displays only the sign of internal functions and hides the underlying complication of data, provides flexibility to change the implementation as per behavior, partial abstraction for achieved abstract classes, and total abstraction for interfaces.

Abstract class: It is a restricted class that cannot be utilised to produce objects (to access it, it must be inherited from another class).

Abstract Method: Only abstract classes can use abstract methods, which are devoid of bodies. The class’s subclass gives the body (inherited from).

Both abstract and conventional methods may be present in an abstract class:

Example for Abstraction Class

//Abstract class

abstract class Animal {

  // Abstract method (Now it has no body)

  public abstract void animalSound();

  // Regular method

  public void sleep() {

    System.out.println(“Zzz”);

  }

}

// Subclass (inherit from Animal)

class Pig extends Animal {

  public void animalSound() {

    System.out.println(“The pig says: wee wee”);

  }

}

class Main {

  public static void main(String[] args) {

    Pig myPig = new Pig(); // It’s a Pig object

    myPig.animalSound();

    myPig.sleep();

  }

}

Encapsulation

Encapsulation enables the developers to protect the data stored in a class from system-range access. It protects the internal content of a class like a capsule and we can implement it in Java for keeping fields private. It can be shared using getter and setter methods and Java Beans is an example of it. It restricts direct access of data members, fields will be set as private, getter, and setter methods for each field, getter method to return the field, and setter method for changing the field value.

Why use Encapsulation?
  • Improved command over class attributes and methods
  • If you only utilize the get method, class attributes can be set to read-only or write-only (if you only use the set method)
  • Programmer-friendly: One modification to the code won’t have an impact on the rest of it.
  • Improved data security
Example for Encapsulation

public class Main {

  public static void main(String[] args) {

    Person myObj = new Person();

    myObj.setName(“Preetha”); 

    System.out.println(myObj.getName());

  }

}

// Prints “Preetha”

Inheritance

Inheritance is the concept that makes it possible to create a derived class to inherit the methods and properties of the base class and allows method overriding. It will also add new data or functions to its base or parent class. The class that shares data and method is called a superclass or parent class or base class. The class that is derived from a method or data is called a child class or subclass or derived class. It improves code reusability; implements the DRY (Don’t repeat yourself) principle, and is extended without limits. It supports single, multilevel, hybrid, and hierarchical inheritance and it does not have multiple inheritances.

Example for Inheritance

class Vehicle {

  protected String brand = “Tata”;        // Attribute of Vehicle class

  public void honk() {                    // Method of Vehicle Class

    System.out.println(“Tuut, tuut!”);

  }

}

class Car extends Vehicle {

  private String modelName = “Mustang”;    // Car attribute

  public static void main(String[] args) {

    // Create a myCar object

    Car myCar = new Car();

// For the myCar object, call the honk() function from the Vehicle class.    myCar.honk();

// Print the modelName value from the Car class and the brand property value (both from the Vehicle class).

    System.out.println(myCar.brand + ” ” + myCar.modelName);

  }

}

Polymorphism

Polymorphism can perform actions in different ways and it can be taken in two forms such as method overloading and method overriding. Method Overloading will be used in various methods that have the same name present in a class and when it is called, they will be different by number, order, and parameters. Method Overriding will occur when the derived or child class overrides a method of base or parent class.

Example for Polymorphism

class Animal {

  public void animalSound() {

    System.out.println(“The animal makes a sound”);

  }

}

class Pig extends Animal {

  public void animalSound() {

    System.out.println(“The pig says: wee wee”);

  }

}

class Dog extends Animal {

  public void animalSound() {

    System.out.println(“The dog says: bow wow”);

  }

}

class Main {

  public static void main(String[] args) {

    Animal myAni = new Anil();  // Creating an Object for Animal

    Animal myPig = new Pig();  // Create a Pig object

    Animal myDog = new Dog();  // Dog object is created

    myAni.animalSound();

    myPig.animalSound();

    myDog.animalSound();

  }

}

Conclusion

Oops basics, the concept helps Java to form a structure with the proper components and architecture. It fastens the application development and deploys error-free code quickly and efficiently. Learn comprehensive Java training in Chennai at SLA to become a master in software development using Java Programming Language with OOPs Certification .

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.