OOP stands for Object Oriented Programming. Object-oriented programming is about creating objects that contain both data and methods that perform actions on that data.
Advantages of OOP:
OOP Concepts:
Abstraction
Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. For example, when you login to your facebook account, you enter your email and password and press login, what happens when you press login, how the input data sent to server, how it gets verified is all abstracted away from the you. Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces. Hide certain details and only show the important details of an object.
public class Main { public static void main(String[] args) { User programmer = new Progammer(); programmer.eat(); programmer.run(); User nflPlayer = new NflPlayer(); nflPlayer.eat(); nflPlayer.run(); } } class Progammer extends User { @Override public void run() { System.out.println("walk..."); } @Override public void eat() { super.eat(); System.out.println("eat healthy..."); } } class NflPlayer extends User { @Override public void run() { System.out.println("run faster and faster..."); } @Override public void eat() { super.eat(); System.out.println("eat a lot for muscle..."); } } abstract class User { // Abstract method (does not have a body) public abstract void run(); // Regular method public void eat() { System.out.println("eat"); } }
Encapsulation
The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must:
The getter method returns the variable value, and the setter method sets the value.
Why use Encapsulation?
get
method), or write-only (if you only use the set
method)public class Main { public static void main(String[] args) { User programmer = new Progammer(); programmer.eat(); programmer.run(); programmer.setId(1); User nflPlayer = new NflPlayer(); nflPlayer.eat(); nflPlayer.run(); nflPlayer.setId(2); } } class Progammer extends User { @Override public void run() { System.out.println("walk..."); } @Override public void eat() { super.eat(); System.out.println("eat healthy..."); } @Override protected int getId() { // TODO Auto-generated method stub return super.getId(); } @Override protected void setId(int id) { // TODO Auto-generated method stub super.setId(id); } } class NflPlayer extends User { @Override public void run() { System.out.println("run faster and faster..."); } @Override public void eat() { super.eat(); System.out.println("eat a lot for muscle..."); } @Override protected int getId() { // TODO Auto-generated method stub return super.getId(); } @Override protected void setId(int id) { // TODO Auto-generated method stub super.setId(id); } } abstract class User { protected int id; // Abstract method (does not have a body) public abstract void run(); // Regular method public void eat() { System.out.println("eat"); } protected int getId() { return this.id; } protected void setId(int id) { this.id = id; } }
Inheritance
it is possible to inherit attributes and methods from one class to another. Inheritance has two components: subclass and super class. To inherit from a class, use the extends keyword.
class Progammer extends User { @Override public void run() { System.out.println("walk..."); } @Override public void eat() { super.eat(); System.out.println("eat healthy..."); } @Override protected int getId() { // TODO Auto-generated method stub return super.getId(); } @Override protected void setId(int id) { // TODO Auto-generated method stub super.setId(id); } }
Why use Inheritance?
It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.
Polymorphism
Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance. Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
class NflPlayer extends User { @Override public void run() { System.out.println("run faster and faster..."); } @Override public void eat() { super.eat(); System.out.println("eat a lot for muscle..."); } @Override protected int getId() { // TODO Auto-generated method stub return super.getId(); } @Override protected void setId(int id) { // TODO Auto-generated method stub super.setId(id); } }