Monday, June 27, 2016

Abstraction

Abstraction  concept seems to like hiding data from outside world.

" Hiding internal implementation and just highlight the set of services what we are offering to the  outside world" its  Abstraction.

Abstraction is achieved by using interface and abstract class. interface gives 100% abstraction.

Example : When we go to the ATM. there we can see only GUI screen. bank management highlights the set of services like cash withdrawal, balance inquiry etc but we do not know what are the process is done internally. they highlight services only without sharing internal implementation.



Advantage :

1.It provides security for application.
2.Enhancement is easy.
3.It improves maintainability of the application
4.It improves easiness to use.


package example;

class Employee extends Person {
    
    private String empCode;

    public String getEmpCode() {
        return empCode;
    }
    public void setEmpCode(String empCode) {
        this.empCode = empCode;
    }
    
}

abstract  class Person {
    
     private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

public class Main{
public static void main(String args[]){
        //INSTIATING AN ABSTRACT CLASS GIVES COMPILE TIME ERROR
        //Person p =  new Person() ;
       
        //THIS REFERENCE VARIABLE CAN ACESS ONLY THOSE METHOD WHICH ARE OVERRIDDEN
        Person person = new Employee();
        person.setName("Indrajeet Singh");        
        System.out.println(person.getName());
    }

}


<<       >>

Data hiding

Data hiding is the most important concepts in OOPs oriented programming. Java security is also depend on Data Hiding concepts.


Its just way to protect your data from the outside world. meanwhile outside person can not access our data its mean our internal data should not go out directly this oops feature is called data hiding.

after validation or authentication of outside world can access our internal data.

if we have a class having public data field or methods. So data member can be accessed by outside person. here, there is no security for internal data.

for hiding our data from outside person , we need to declare our data as private or protected.

 such an example :  If we have an account in a bank. but we are not able to access our account information for accessing our information regarding account, So first bank will provide login authentication your user name and password. then you can check your account information.


        public class Account {
                             private double balance;
                             public double getBalance()
                             {
                               // validation
                                return balance.
                              }
                            }


Advantage of Hiding data for security. So its highly recommended to declare data member variable as private.


<<         >>