4 Characteristics of OOP
Encapsulation- A method of making the form of data and code unknown to the outside, and structuring the data's structure, role, and function into a single capsule.
Abstraction- Expressing common characteristics (variables, methods) of classes by grouping them together.
Inheritance- Using variables and methods defined in a parent class by inheriting them in a child class.
Polymorphism- Refers to a structure that can be expressed in various forms.
Encapsulation #
A method of making the form of data and code unknown to the outside, and structuring the data's structure, role, and function into a single capsule (information hiding).
- Encapsulation Methods
- Attach the access modifier
privatebefore member variables. (private: accessible only within its own class) - Create methods to set and retrieve values for member variables (getter, setter).
- Attach the access modifier
Abstraction #
Expressing common characteristics (variables, methods) of classes by grouping them together.
Inheritance #
Using variables and methods defined in a parent class by inheriting them in a child class.
e.g.) Using the extends keyword
Polymorphism #
The ability for an object to respond in its own unique way to a single message when performing an operation based on that message.
- Methods Supporting Polymorphism
- Overloading
- Overriding
Overloading #
Defining multiple methods with the same name within a single class.
Overriding #
Changing and using the content of a method inherited from a parent class. Parameters and return type must be the same.