Tuesday, August 19, 2014

JAVA OOP Concepts Demo - Encapsulation and Polymorphism (Overloading)

// Object Oriented Programming Principles
//
//      1. Encapsulation - A process of combining the data and functions that  manipulates the data.
//
//                Encapsulation = Data + Functions
//
//                Java Implementation  :
//                    Class = Variables + Methods
//
//      2. Polymorphism - one interface, multiple methods
//            
//                Java Implementation - Overloading :
//                     Define two or more methods with the same name but different signatures
//                     in the same class.

====================================================================
package encapsulation.and.overloading;

public class Point {
    private int x;
    private int y;
   
    public Point() {
        x = y =  0;
    }
   
    public Point(int x, int y) {
        setX(x);
        setY(y);
    }
   
    public void setX(int x) {
        this.x = x;
    }
   
    public void setY(int y) {
        this.y = y;
    }
   
    public int getX() {
        return x;
    }
   
    public int getY() {
        return y;
    }
   
    public void move() {
        x++;
        y++;
    }
   
    public void move(int distance) {
        x += distance;
        y += distance;
    }
   
    public void move(int xdistance, int ydistance) {
        x += xdistance;
        y += ydistance;
    }
}

No comments:

Post a Comment