×
>
<

Aptitude

Java Oops | CrackEase

Introduction to Java

Java OOPs concepts

Java OOPs Concepts

This page introduces the basics of Object-Oriented Programming (OOP) in Java. OOP is a programming paradigm built around objects and classes, offering key concepts such as inheritance, polymorphism, abstraction and encapsulation.

Simula is often cited as the first object-oriented language. In true OOP languages, most program elements are represented as objects. The main goal of OOP is to model real-world entities (objects) and their interactions using classes, making code modular, reusable and easier to maintain.

What is OOPs (Object-Oriented Programming System) ?

An object is a real-world entity such as a pen, chair, table, computer, or watch. Object-Oriented Programming is a methodology to design software using classes and objects. It simplifies development and maintenance by encouraging clear structure and reuse through the following core concepts:

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

There are also related design terms frequently used in OOP:

  • Coupling
  • Cohesion
  • Association
  • Aggregation
  • Composition

Object

An object is any entity that has state and behavior. Examples: a chair, a pen, a car, or a user account. An object is an instance of a class; it occupies memory and has an address. Objects interact by sending messages (calling methods) without needing to know each other's internal implementation — only the interface matters.

Example: A dog is an object — it has states (color, name, breed) and behaviors (bark, eat, wagTail).

Class

A class is a blueprint or template used to create objects. It defines fields (data) and methods (behavior). Unlike objects, a class itself does not consume instance memory until objects (instances) are created from it.

Inheritance

Inheritance allows a class (subclass/child) to acquire properties and behaviors of another class (superclass/parent). It promotes code reuse and establishes an "is-a" relationship. Java supports single inheritance for classes and multiple inheritance via interfaces.

Polymorphism

Polymorphism means "many forms" — the same operation can behave differently on different classes. In Java, polymorphism is achieved through method overloading (compile-time) and method overriding (runtime).

Example: A draw() method might render different shapes (circle, rectangle, triangle) depending on the object's type.

Abstraction

Abstraction means hiding internal details and showing only essential features. In Java, abstraction can be achieved using abstract classes and interfaces. Abstraction helps reduce complexity and lets programmers focus on high-level design.

Encapsulation

Encapsulation is the practice of bundling data (fields) and methods that operate on the data into a single unit (class), and restricting direct access to some of the object's components. Access modifiers (private, protected, public) are used to enforce encapsulation.

A Java Bean is an example of a fully encapsulated class: fields are private and accessed through public getters/setters.

Design terms (Coupling, Cohesion, Association)
1. Coupling

Coupling measures how much one class depends on another. Low (loose) coupling is preferred because it makes code easier to maintain and test. Use interfaces and dependency injection to reduce coupling.

2. Cohesion

Cohesion measures how related the responsibilities of a single module or class are. High cohesion (a class focused on a single responsibility) is desirable; it improves readability and maintainability.

3. Association

Association represents relationships between objects. It can be:

  • One to One
  • One to Many
  • Many to One
  • Many to Many

Associations can be unidirectional or bidirectional. Real-world examples: a country has a prime minister (one-to-one), a teacher has many students (one-to-many).

4. Aggregation

Aggregation is a weak "has-a" relationship where one object contains others but they can exist independently (e.g., a department has employees).

5. Composition

Composition is a strong form of aggregation where contained objects cannot exist independently of the parent (e.g., a house has rooms — rooms don't exist without the house). Composition implies lifecycle dependency.

Footer Content | CrackEase