OOP
Object Oriented Programming is a programming paradigm that organizes data and behaviors into reusable and self-contained units called objects.
Class
A blueprint or template for creating objects.
public class Person {
// Attributes (instance variables)
String name;
int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Method
public void introduce() {
System.out.println("Hello, I'm " + name + " and I'm " + age + " years old.");
}
}Object
Instance created from a class
Interface
A blueprint for a class that defines a set of methods (function signatures) that must be implemented by any concrete class that implements the interface
Class implements Interface
'Circle implements Drawable' it means that the Circle class is declaring that it will adhere to a contract defined by the Drawable interface = It agrees to provide concrete implementations for all the methods declared in that interface.
4 fundamental principles of OOP
Inheritence
Polymorphism
Encapsulation
Abstraction
Inheritence
Child inherits parent's properties and methods ("is a" relationship)
Polymorphism
"many forms" in OOP
Can just use their abstract type "Shape" instead of actual Shape
Encapsulation
Bundle data of object into a single unit "class" and restrict access to internal state / data of object
More controlled access to state of object through getters and setters
Abstraction
Simplifying complex systems by focusing on what an object does (its behaviors) while hiding how it does it (implementation details).
eg. A Shape interface defines methods like draw() and resize() without specifying how each shape is drawn or resized.
Last updated