Showing posts with label encapsulation. Show all posts
Showing posts with label encapsulation. Show all posts

Saturday, April 2, 2016

Java OOP - Encapsulation


Encapsulation is the process of wrapping code and data together into a single unit. "But, hey Rolo how do we do this?"

To achieve encapsulation in your beautiful Java code:

  • Declare the variables of a class private
  • Provide public setter and getter methods to modify and view the variables values.
So the code can go as follow:


/* File name : EncapTest.java */
public class EncapTest{

   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;
   }
}


In order to avoid any other class to modify our encapsulated class, variables are declared as private and to get access, should be through its public getters and setters.

This is how we access:

/* File name : RunEncap.java */
public class RunEncap{

   public static void main(String args[]){
      EncapTest encap = new EncapTest();
      encap.setName("James");
      encap.setAge(20);
      encap.setIdNum("12343ms");

      System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
    }
}

Benefits?

  • Easy to model real-world entities
  • Easy to maintain and understand
  • Control how we access data and how is modified
  • Reusability
  • Flexibility
  • Secure

This short video will help to clarify this concept. Enjoy!







Wednesday, March 30, 2016

Java Object Oriented Programming (OOP)



Java is so popular nowadays thanks to many features that help developers code faster and efficiently.
One of those important features is : Object Oriented Programming or OOP

But What does it mean? Basically, in Java, everything is an Object. Java can be easily extended since it it based on the Object model. Something like picturing everything as it happens in real world.

For example:

A car is assembled from parts and components, such as chassis, doors, engine, wheels, brake and transmission. The components are reusable, e.g., a wheel can be used in many cars (of the same specifications).

Hardware, such as computers and cars, are assembled from parts, which are reusable components.

How about software? Can you "assemble" a software application by picking a routine here, a routine there, and expect the program to run? The answer is obviously no! Unlike hardware, it is very difficult to "assemble" an application from software components. Since the advent of computer 70 years ago, we have written tons and tons of programs. However, for each new application, we have to re-invent the wheels and write the program from scratch.

As a solution for all this "assemble" and sharing issues. we have Java OOP features. yes! Java saving our life, one more time!!!!

Some of Java OOP concepts are:
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction



oops concept in java




But,"hold on, Rolo, are you saying that we need to change our reliable way to code and leave our comfort and start coding in OO way?"...No! We live in a free country, you don't have to,.lol...However, there are some huge advantages about coding in Java and using its OOP features.....

Benefits of OOP:
  • OOP provides a clear modular structure for programs. 
  • It is good for defining abstract data types. 
  • Implementation details are hidden from other modules and other modules has a clearly defined interface. 
  • It is easy to maintain and modify existing code as new objects can be created with small differences to existing ones. 
  • It implements real life scenario. 
  • More reliable software development is possible. 
  • Much suitable for large projects.

Well, hope you got a good overview about what OOP is. I'll post about every OO concept later....