MD Pabel
Back to Notes

Object oriented programming (OOP)

MD Pabel

MD Pabel / Mar 28, 2024

7 min read

Table of contents

Classes and Objects

A class is a blueprint for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). An object is an instance of a class. Objects are instantiated from classes using the new keyword. A constructor in Java is a block of code similar to a method that's called when an instance of an object is created. Java allows multiple constructors as long as they have different parameters, this is called constructor Overloading.

Memory Allocation:

In Java, memory for objects is allocated on the heap when they are created.

Static members:

Static members belong to the class rather than any object instance, allowing Java to create methods and variables that belong to the class as a whole.

Loading code...

this:

The this keyword in Java is a reference variable that refers to the current object.

  1. To Refer to Current Object's Instance Variables
  2. To Invoke Current Object's Method
  3. To Pass Current Object as a Parameter
  4. To Return the Current Class Instance
  5. To Invoke Current Object's Constructor

Method chaining:

Method chaining is a common syntax for invoking multiple method calls in object-oriented programming languages.

1. Encapsulation

Encapsulation is the bundling of data with the methods that operate on these data. The data is hidden from outside the class and can only be accessed through methods, which provides control over how the data can be manipulated and prevents unintended modification or access, these methods are called (public) setter and getter methods.

Loading code...

2. Abstraction

Abstraction in Java refers to hiding the implementation details of a code and exposing only the necessary information to the user.

abstract keyword

Loading code...

Interfaces

Interfaces define a contract for classes to implement. They declare method signatures without providing implementations. Classes that implement an interface must provide implementations for all the methods declared in the interface.

Loading code...

Differentiating Interfaces and Abstract Classes:

Interfaces cannot hold state and provide a contract for what a class can do. Abstract classes can hold state and can provide some implementation.

Access Modifiers

Access modifiers such as public, protected, and private can also be used to achieve abstraction by controlling the visibility of members (fields and methods) of a class.

Loading code...
  1. public: Members (fields, methods, classes) marked as public are accessible from any other class.
  2. protected: Members marked as protected are accessible within the same package or by subclasses (even if they are in different packages).
  3. private: Members marked as private are accessible only within the same class.

Coupling

Coupling refers to the degree of direct knowledge that one element has of another. Use interfaces and dependency injection to reduce coupling.

Dependency Injection:

A technique where one object supplies the dependencies of another object, promoting loose coupling.

3. Inheritance

  • Allows a class to inherit properties and methods from another class.
  • Constructors of the base class can be called in the derived class using super().
Loading code...

Upcasting and Downcasting:

Refers to casting a subclass object to a superclass reference and vice versa.

Loading code...

Final Classes and Methods:

Prevent classes and methods from being inherited/overridden.

Multiple Inheritance:

Java does not support multiple inheritance with classes to avoid complexity and diamond problem.

4. Polymorphism

Ability of an object to take on many forms. It's a core concept that allows one interface to be used for a general class of actions. We can implemenet using method overriding where a child class overrides a method of its parent class.

There are two main types of polymorphism:

Compile-Time Polymorphism (Method Overloading):

Method overloading allows a class to have multiple methods with the same name but with different parameters. The decision about which method to call is made at compile-time based on the number and type of arguments provided. It enables the same method name to perform different actions depending on the context.

Loading code...

Run-Time Polymorphism (Method Overriding):

Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. The decision about which method to call is made at runtime based on the type of the object.

Loading code...

Nested classes in Java are classes that are defined within another class.

Loading code...

References:

  1. https://codewithmosh.com/p/ultimate-java-part-2

OOP certificate