Object Oriented Concepts in Java – OOP

In this Java OOP tutorial we want to learn about Object Oriented Concepts in Java, so Java is an object oriented programming language, it means that it is designed around the concepts of objects, classes and inheritance. if we understand basic principles of Object Oriented Programming, than we can write clean and maintainable code, so now let’s talk about some basics of Java OOP.

 

 

  1. Java Classes and Objects

In Java everything is an object, and objects are created from classes. class is a blueprint or a template that defines the properties and behaviors of an object. also an object is an instance of a class. this is the basic syntax for creating a class:

 

 

Basic Syntax for Creating a Class

 

 

 

Creating and Using a Class and Object

Now let’s create a class named Person and an object from this class. now in here we want to create a Person class in Java.

 

 

 

Now let’s create object if Person class in Java OOP.

 

 

 

If you run your code, this will be the result

Object Oriented Concepts in Java - OOP
Object Oriented Concepts in Java – OOP

 

Java Class Code Description

  1. Class Definition: We have defined Person class with two attributes: name and age. also the class has a constructor to initialize these attributes and a method displayDetails to print the details of a person.
  2. Constructor: Our constructor Person(String name, int age) initializes the name and age attributes using the this keyword, which refers to the current object.
  3. Method: displayDetails method prints the name and age of the person.
  4. Creating an Object: In the Main class, we create an object of the Person class using the new keyword and call the displayDetails method on this object.

 

 

 

Key Points on Java Class

  • Class: A blueprint that defines attributes and methods.
  • Object: An instance of a class created using the new keyword.
  • Constructor:  Special method used to initialize objects.
  • Method: Function defined within a class to perform a specific action.

 

 

 

 

 

  1. Java Encapsulation

Encapsulation is the concept of hiding internal details of an object and exposing only the necessary functionalities via public interface. Using encapsulation you can protect the state of an object and prevent it from being accessed or modified by external entities. for creating encapsulation we can use access modifiers such as public, private and protected.

 

 

Basic Syntax for Java Encapsulation

For encapsulating data, you generally:

  1. Declare the class variables as private.
  2. Provide public getter and setter methods to access and update the private variables.

 

 

Using the Encapsulated Person Class

 

 

Code Description

  1. Private Attributes: our Person class has private attributes of name and age. These attributes cannot be accessed directly from outside of  class.
  2. Constructor: constructor Person initializes the name and age attributes.
  3. Getter and Setter Methods: Public getter and setter methods are provided for accessing and modifying the private attributes. These methods ensures that internal state is accessed and modified in controlled manner.
    • getName and getAge methods return the values of name and age.
    • setName method updates the name attribute.
    • setAge method updates age attribute, but only if provided age is positive. This adds a layer of validation.
  4. Displaying Details: displayDetails method prints the name and age of the person.

 

 

Key Points on Java Encapsulation

  • Access Modifiers: Use private for hiding attributes from external access, and public for methods that need to be accessible.
  • Data Protection: Encapsulation protects the state of an object by providing controlled access through getter and setter methods.
  • Validation: Using encapsulation you can add validation logic in setter methods to ensure the integrity of the data.

 

 

 

  1. Java Inheritance

Using inheritance one class inherits the properties and behaviors of another class. for the class that is being inherited we can call superclass or parent class, and the class that inherits from, it is called subclass or child class. inheritance allows us to reuse code and create hierarchy of classes with increasing levels of abstraction.

 

 

 

Basic Syntax for Inheritance

For implementing inheritance in Java, you use extends keyword.

 

 

Let’s create an example demonstrating inheritance in Java.

 

Superclass: Animal

 

 

Subclass: Dog

 

 

Using Animal and Dog Classes

 

 

Code Description

  1. Superclass (Animal):
    • Animal class has a property name, a constructor to initialize this property, and two methods: displayDetails and makeSound.
    • makeSound method in Animal provides a general behavior for making a sound.
  2. Subclass (Dog):
    • Dog class extends the Animal class, and it inherits its properties and methods.
    • It has an additional property breed and a constructor that calls the superclass constructor using super(name) to initialize the name property.
    • makeSound method is overridden to provide a specific implementation for dogs.
    • displayDetails method is also overridden to include the breed information.
  3. Main Class:
    • In the Main class, an Animal object and a Dog object are created.
    • displayDetails and makeSound methods are called on both objects to demonstrate inheritance and method overriding.

 

 

 

Key Points on Java Inheritance

  • Inheritance Syntax: We need to use extends keyword for creating subclass.
  • Superclass and Subclass: superclass contains general properties and methods, and subclass can add specific properties and methods or override existing ones.
  • Code Reuse: Using java inheritance we can reuse our code, and that is because subclasses inherits and build upon the code in the superclass.
  • Method Overriding: Subclasses can provide specific implementations of methods that are defined in the superclass, allowing for polymorphic behavior.

 

 

 

 

  1. Java Polymorphism

Using polymorphism an object can take many forms. in Java we can create polymorphism using method overriding and method overloading. method overriding is used when subclass provides its implementation of a method that is already defined in its superclass. method overloading is used when a class has two or more methods with the same name but different parameters.

 

 

Java Method Overriding

Using method overriding, we have specific implementation for a method that is already defined in its superclass. This is useful for defining behavior that is specific to the subclass.

 

 

Example of Method Overriding

 

 

 

Run the code and this will be the result

Java Method Overriding
Java Method Overriding

 

 

Code Description for Method Overriding

  1. Superclass (Animal): Animal class has a method makeSound that prints a generic message.
  2. Subclass (Dog): Dog class extends Animal and overrides the makeSound method to provide a specific implementation that prints a message for a dog.
  3. Main Class: In the Main class, we have created objects of Animal and Dog. When the makeSound method is called on the Dog object, the overridden method in the Dog class is executed, demonstrating polymorphism.

 

 

 

Java Method Overloading

Method overloading allows a class to have more than one method with the same name, but with different parameters. This is useful for providing multiple ways to perform a similar operation.

 

 

Example of Method Overloading

 

 

Method Overloading

  1. MathOperations Class: MathOperations class defines multiple add methods, each with different parameters.
    • first add method takes two integers.
    • second add method takes three integers.
    • third add method takes two double values.

 

 

 

This will be the result

Java Method Overloading
Java Method Overloading

 

 

 

  1. Java Abstraction

Abstraction is the process of identifying the essential features of an object while ignoring the non-essential details. Using that we can focus on what an object does rather than how it does it. In Java, abstraction can be achieved using abstract classes and interfaces.

  • Abstract Class: An abstract class is a class that cannot be instantiated and can only be used as a superclass for its subclasses. It can have both abstract methods (without a body) and concrete methods (with a body).
  • Interface: An interface is a collection of abstract methods and constants. A class that implements an interface must provide implementations for all the abstract methods defined in the interface.

 

 

 

Abstract Class Example

An abstract class can have abstract methods, and do not have a body, and concrete methods, which have a body. Subclasses of an abstract class must provide implementations for the abstract methods.

 

 

 

This will be the result

Java Abstract Class
Java Abstract Class

 

 

 

Java Interface

An interface defines a contract for what a class can do, without specifying how it does it. Classes that implement an interface must provide implementations for all its methods.

 

 

This is an example of Java Interface

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×