Difference between revisions of "Object-Oriented Concepts and Constructs"

From Sinfronteras
Jump to: navigation, search
(Encapsulation)
Line 6: Line 6:
  
 
===Encapsulation===
 
===Encapsulation===
 +
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class, therefore it is also known as data hiding.
 +
 +
To achieve encapsulation in Java:
 +
* Declare the variables of a class as private.
 +
* Provide public setter and getter methods to modify and view the variables values.
 +
 +
TestEncapsulation.java
 +
<syntaxhighlight lang="java">
 +
public class TestEncapsulation{
 +
   
 +
    private String name;
 +
    private String idNum;
 +
    private int age;
 +
 
 +
    public int getAge(){
 +
        return age;
 +
    }
 +
   
 +
    public String getName(){
 +
        return name;
 +
    }
 +
   
 +
    public String getIdNum(){
 +
        return idNum;
 +
    }
 +
   
 +
    public void setAge(int newAge){
 +
        age = newAge;
 +
    }
 +
   
 +
    public void setName(String newName){
 +
        name = newName;
 +
    }
 +
   
 +
    public void setIdNum( String newId){
 +
        idNum = newId;
 +
    }
 +
   
 +
}
 +
</syntaxhighlight>
 +
 +
The public setXXX() and getXXX() methods are the access points of the instance variables of the EncapTest class. Normally, these methods are referred as getters and setters. Therefore any class that wants to access the variables should access them through these getters and setters.
  
 
===Inheritance===
 
===Inheritance===

Revision as of 16:34, 18 March 2018

Four fundamental OOP concepts

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Encapsulation

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class, therefore it is also known as data hiding.

To achieve encapsulation in Java:

  • Declare the variables of a class as private.
  • Provide public setter and getter methods to modify and view the variables values.
TestEncapsulation.java
public class TestEncapsulation{
    
    private String name;
    private String idNum;
    private int age;
   
    public int getAge(){
        return age;
    }
    
    public String getName(){
        return name;
    }
    
    public String getIdNum(){
        return idNum;
    }
    
    public void setAge(int newAge){
        age = newAge;
    }
    
    public void setName(String newName){
        name = newName;
    }
    
    public void setIdNum( String newId){
        idNum = newId;
    }
    
}

The public setXXX() and getXXX() methods are the access points of the instance variables of the EncapTest class. Normally, these methods are referred as getters and setters. Therefore any class that wants to access the variables should access them through these getters and setters.

Inheritance

Polymorphism

Abstraction