The Four Principles of OOP Java
I’ve been programming for roughly 7 months now and I can’t express how happy I am that I made this transition. I found myself finding a love in problem solving and critical thinking. As well as the freedom that comes with designing your own application!
As I continue to venture on this journey I find myself in a very fortunate position where I will be able to continue my studies and pursue a Computer Science degree.
I’ve been looking through the courses I need to take to complete my degree at NOVA and noticed I will be needing to learn another language being Java. So I’m taking it upon myself to do some research before taking my classes and figured I’d share along the way!
Today I will be covering the four principle of Object Oriented Programming in Java. Encapsulation, Inheritance, Polymorphism, and Abstraction.
1. Encapsulation
Encapsulation is wrapping data(variables) and functionality(methods) together as a single unit. Functionalities mean “methods” and data means “variables”. It’s all wrapped in is “class.” It is a blueprint or a set of instruction.
Class: A class is a blueprint or prototype that defines the variables and the methods. For example:
Class: Car
Data members or objects: color, type, model, etc.
Methods: stop, accelerate, cruise.
Object: Now, an object is a specimen of a class. Like in the above example my car is an object of the class Car.
Variable: can be local, instance and static. Local variables are declared inside the body of a method. Instance variables are declared outside method. They are object specific. Static variables are initialized only once and at the start of program execution. Static variables are initialized first, we will discuss static in detail later.
Package: A Package is a collection of related classes. It helps organize classes into a folder structure and make it easy to locate and reuse them.
Method: methods are various functionalities, its nothing but set of code which is referred to by name and can be called (invoked) at any point in a program. You can pass multiple values to a method and it returns value(s).
2. Abstraction
Abstraction is selecting data from a larger pool to show only the relevant details to the object. Here is a chart showing different access modifiers and how it restricts the data from a class.
3. Inheritance
Inheritance is a mechanism in which one class acquires the property of another class. For example, a child inherits the traits of his/her parents.
4. Polymorphism
Polymorphism is a OOPs concept where one name can have many forms also knows as overloading. Dynamic Polymorphism is the mechanism by which multiple methods can be defined with same name and signature in the superclass and subclass also known as overriding.
- Overloading is multiple methods in the same class with same name but different method signature.
- Overriding deals with two methods, one in parent class and one in child class and both have same name and signature.
- Subclass method overrides the method from super class.
- In overriding sub classes access modifier must be greater than parent class E.g if we use public abc() in parent class and private abc() in sub class that will throw exception.
I will continue to post as I continue to discover! Happy coding!