Java OOP

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:

  1. Faster to execute
  2. Easier to maintain
  3. Clear program structure
  4. Support DRY “Don’t Repeat Yourself” principle which makes your code easier to read, modify and debug
  5. Reusable and less code and shorter development time

OOP Concepts:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

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:

  • declare class variables/attributes as private
  • provide public getter and setter methods to access and update the value of a private variable

The getter method returns the variable value, and the setter method sets the value.

Why use Encapsulation?

  • Better control of class attributes and methods
  • Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)
  • Flexible: the programmer can change one part of the code without affecting other parts
  • Increased security of data
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);

    }

}

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *