Introduction to OOP

295 2 0
                                    

Object - This is the object of the class. The class properties and methods can be used through this object.

Class - this is the blueprint where your object was based on.

==============================

class HelloWattapad{
    String name = "Shikoku";
   
    public void welcomeMethod(){
        System.out.println("Hi" + greet);
    }
}

==============================

name - this is the class property or sometimes called state. It is a variable with a String data type and an initial value of shikoku.

welcomeMethod() - this is the class method or sometimes called behaviour. The function was just to print Hi shikoku.

Data encapsulation- hiding the state from outside the class and let them access through method.

============================
class Hiding{
    private String girlfriend = "Taylor";

    public String getProperty(){
        return girlfriend;
    }

    public void setProperty(String name){
        girlfriend = name;
    }
}

class AccessingProperty{
    public static void main(String[] args){
      Hiding obj1 = new Hiding();
      System.out.println(obj1.getProperty);

      Hiding obj2 = new Hiding();
      obj2.setProperty("Selena");
    System.out.println(obj2.getProperty());
    }
}
===========================
Result:
Taylor
Selena

private- it is a modifier where access is limited only within the class. By making the property above private, other class cannot access it. The only way to access  is through the method given.

return- it is a keyword used when you want to return something like for example the above code returns a String.   The method's return is no longer void but a data type String.

set and get - This is one of the methods used when you are using data Encapsulation. You can set the property by providing the appropriate parameter as an argument to the method. Then, you can use the get property to get what is being set in the property.

Java ProgrammingWhere stories live. Discover now