Skip to content

Object Orientation


1. Why OOP??

  • OOP helps to keep the Java code DRY "Don't Repeat Yourself"
  • Makes the code easier to comprehend, maintain, modify and debug

2. OOP Terms (1 of 3)

Key terms used when describing Object Oriented Programming/Designing.

|400


2.1. OOP Terms (2 of 3)

TermDescriptionUse Case
EncapsulationBundling data and methods together while restricting direct access to some components.Hiding internal state and exposing only necessary API methods (e.g., getBalance()).
AbstractionHiding complex implementation details while exposing essential features.Providing a simple interface like pay() without showing internal payment logic.

2.2. OOP Terms (3 of 3)

TermDescriptionUse Case
InheritanceAllowing one class to derive from another and reuse its behavior.Employee class inheriting from a Person class.
PolymorphismAbility for different classes to respond differently to the same method call.draw() behaving differently for Circle, Square, and Triangle classes.

3. Class Structure (1 of 2) :: Class, Object and new keyword

TermDescriptionUse Case
ClassA blueprint that defines the properties (attributes) and behaviors (methods) of objects.Used to model real-world entities such as Car, User, or Invoice.
ObjectAn instance of a class containing actual data.Creating a specific User object from a User class with unique data.
The new keyword, creates an Object ("a living copy") of a Class ("a blueprint/template" of Java code grouping).

3.1. An example :: a class

We do new MethodDemo() to create an instance (an Object) of the this class.


3.2. Class Structure (2 of 2) :: Constructors & Destructors

ConstructorA special method called when an object is created to initialize it.Setting default values when creating a new Order.
DestructorA method called when an object is destroyed or goes out of scope.Releasing resources or closing files in languages that support destructors.

4. Methods and Members/Fields

TermDescriptionUse Case
Attribute (Field)A variable inside a class that stores data for objects.name, age, or balance inside a Customer class.
MethodA function defined inside a class to perform actions.deposit(), drive(), or login() actions.

5. "Type Contracts" :: Abstract classes and Interfaces

TermDescriptionUse Case
Abstract ClassA class that cannot be instantiated and may contain abstract methods.Base class for shared logic with required methods, like Animal with an abstract makeSound().
InterfaceA contract listing methods a class must implement without providing implementation.Ensuring multiple classes share consistent behavior like Serializable or Drawable.

6. Code Organisation

TermDescriptionUse Case
Namespace / PackageContainers for organizing related classes.Grouping utility classes under utils or math.

7. Behaviour Modification

TermDescriptionUse Case
OverloadingDefining multiple methods with the same name but different parameters.add(int, int) and add(double, double).
OverridingRedefining a method from a parent class in a child class.Customizing toString() or overriding move() in subclasses.

8. Relationships

TermDescriptionUse Case
AssociationA relationship where two classes interact but neither owns the other.Teacher teaches Student.
AggregationA “has-a” relationship where one class contains another, but both can exist independently.Team has Players.
CompositionA strong form of aggregation where the contained object cannot exist independently.House composed of Rooms.

is-a vs has-a :: Inheritance vs Composition/Encapsulation


Inheritance



9. References

Object-Oriented-Programming Concepts in Java | Baeldung https://www.baeldung.com/java-oop

Java OOP(Object Oriented Programming) Concepts - GeeksforGeeks https://www.geeksforgeeks.org/java/object-oriented-programming-oops-concept-in-java/

Java OOP (Object-Oriented Programming) https://www.w3schools.com/java/java_oop.asp

OOP Exercises - Java Programming Tutorial https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html

Java OOP Cheat Sheet: Object Oriented Programming Concept Codes https://www.edureka.co/blog/cheatsheets/java-oop-cheat-sheet/

© 2023-2025 Maduranga Kannangara. Feel free to use or share this content. Attribution is appreciated but not required.