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); } }
Interface is an abstraction class. Abstraction is the process of showing only the neccessary funtionality and hide the uncessary details.
What is an interface in Java?
An interface can have method signatures, default methods, and variables which are public, static, and final by default.
What is the use of an interface?
In java a class can only inherit a one other class. If you want to inherit two or more classes you need to use interfaces. A class can implement more than one interface.
How to create an interface?
1. Use the interface keyword. It looks like a class but it is not a class.
2. Use method signature only(implementation is hidden in the class that implements the interface).
interface UserAction{ // by default limit is public static final int = 10; int limit = 10; void speak(String message); void eat(String food); }
Example of an Interface
// This class provides the implementation for the UserAction interface class UserService implements UserAction{ @overide void speak(String message){ // logic goes here and can be long System.out.println(message); } @overide void eat(String food){ // logic goes here and can be long System.out.println("I am eating a "+food); } }
public class Main{ public static void main(String[] args) { UserAction userAction = new UserService(); userAction.speak("Hi my name is Folau"); userAction.eat("burger"); } }
Implements Multiple Interfaces
public interface UserAction { int limit = 10; public void speak(String message); public void eat(String food); }
public interface MachineAction { public void run(); }
public class ActionService implements UserAction, MachineAction{ @Override public void run() { System.out.println("run"); } @Override public void speak(String message) { System.out.println(message); } @Override public void eat(String food) { System.out.println("I am eating a "+food); } }
public class Main { public static void main(String[] args) { ActionService actionService = new ActionService(); actionService.eat("burger"); actionService.run(); } }
Everything in Java is either an a primitive like int, double, etc or an object. An object is created from a class.
A class is like an object blueprint, an object constructor, or object template from which objects are created. It defines the behavior and attributes of the object.
After it is set up you can then create objects from it. You can think of it as a car. A car has attributes such as style and color and functions or methods such as drive and stop.
To create a class:
1. Use the keyword class
2. Use an uppercase class name
class User{ String name; public void sayHi(){ System.out.println(name); } }
To create an Object
public class Main{ public static void main(String[] args) { User user = new User(); user.name = "Folau"; System.out.println(user.name);// Folau // call method user.sayHi();// Folau } }
Method is a block of code which runs only when it is called or invoked.
Method is used to perform certain behaviors or actions.
Method can accept data which are called parameters.
To create a method, you need the follow things:
1. name
2. return type
3. parameters
4. statements
5. return value
int addOne(int num){ int answer = num +1; return answer; }
Methods must be inside of a class.
class MathClass{ int addOne(int num){ int answer = num +1; return answer; } }
Calling a method
A method can be called multiple times.
class MathClass{ static int addOne(int num){ int answer = num +1; return answer; } public static void main(String[] args) { int answer = addOne(5); System.out.println("answer is "+answer);// 6 } }
Method Parameters
You can pass in however many parameters you would like just separate them by comma.
int add(int num1, int num2, int num3, int num4){ int answer = num1 + num2 + num3 +num4; }
Return values
Our example above indicates that add must return an int. Every method must return either a value or void.
void sayHello(String name){ System.out.println("Hello "+name+"!"); }
sayHello method has a return type of void.