Object Oriented Programming Principles

Object-oriented programming (OOP) is at the core of Java. All Java programs are to at least some extent object-oriented. OOP is so integral to Java that it is best to understand its basic principles before you begin writing even simple Java programs. Therefore, this chapter begins with a discussion of the theoretical aspects of OOP.

Java OOPs Concepts - Javatpoint

Class and object

a class is a blueprint or template for creating objects. It defines the properties (attributes) and behaviours (methods) that objects of that class will have. A class is like a blueprint that specifies the characteristics of objects that can be created from that class.

Object Oriented Programming 101 — Classes and Objects | by SUMIT SHARMA |  Geek Culture | Medium

Inheritance

Superclass will provide a separate copy of properties to every object of the subclass. This process is called Inheritance. Inheritance is also known as an “IS-A relationship”. Inheritance can be achieved by using the keyword “extends”. Only non-static members can be inherited, static members cannot be inherited but rather shared.

There are 4 types of Inheritance.

a.) Single-level Inheritance

b.) Multi-level Inheritance

c.) Hierarchical Inheritance

d.) Multiple Inheritance: This cannot be achieved using classes.

Example:-

the parent class inherits from the GrandParent class using the extends keyword and the The child class inherits from the Parent class using the same keyword. Therefore, the Child class has access to the properties and methods of both the Parent and GrandParent classes.

Inheritance in Python | Python Inheritance - Scaler Topics

Polymorphism

It allows objects to take on different forms, based on the context in which they are used. In Java, there are two types of polymorphism:

a)compile-time polymorphism (also known as method overloading).

b) runtime polymorphism (also known as method overriding).

1) Compile-time Polymorphism:

Compile-time polymorphism is achieved through method overloading, where multiple methods with the same name but different parameters are defined in the same class. During compilation, the Java compiler selects the appropriate method to call based on the arguments provided to it.

Run Time Polymorphism vs Compile Time polymorphism - Java Training School

2) Run time polymorphism:

runtime polymorphism is achieved through method overriding. Method overriding is the ability of a subclass to provide its implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.

What are the practical (real life) examples of polymorphism, inheritance,  composition, overriding, encapsulation, abstraction and other important  concepts of OOPS? - Quora

Encapsulation

Encapsulation in Java is the process of binding data and the methods that operate on that data within a single unit or entity, and controlling access to that entity from outside the class. In Java, encapsulation is achieved through the use of access modifiers, which are keywords that control the visibility and accessibility of methods and variables in a class.

There are four access modifiers in Java:

  1. Public: A public method or variable is accessible from any class in the program.

  2. Private: A private method or variable is only accessible within the class where it is defined.

  3. Protected: A protected method or variable is accessible within the same class, as well as any subclass of that class.

  4. Default: A default method or variable is accessible within the same package.

To achieve encapsulation, the instance variables of a class are usually declared as private, so that they cannot be accessed directly from outside the class. Access to these variables is provided through public methods called getters and setters, which allow controlled access to the data.

Getters are used to access the value of an instance variable, while setters are used to set the value of an instance variable. By encapsulating the instance variables, the internal details of the object are hidden from the outside world, and the object can only be accessed through its public methods, which ensures data integrity and security.

Why do we need Encapsulation in Java? - Use My Notes

Abstraction

abstraction is a way of hiding the implementation details of a class while providing a simplified and standardized interface for other parts of the program to interact with.

Abstraction is achieved in Java through the use of abstract classes and interfaces. An abstract class is a class that cannot be instantiated and is typically used as a base class for other classes. Abstract classes may have abstract methods, which are methods that have no implementation and must be defined by any concrete subclass that extends the abstract class.

An interface in Java is a collection of abstract methods that can be implemented by any class that implements the interface. Interfaces provide a standardized way of interacting with objects that implement them, regardless of their specific implementation details.

By using abstraction in Java, we can create more modular and maintainable code. Abstraction allows us to separate the concerns of our code, making it easier to understand, test, and modify. It also allows us to write code that is less tightly coupled, meaning that changes to one part of the codebase will have less of an impact on other parts of the codebase.

When Do We Use an Abstract Class in Java? - Scaler Topics