Fallowing icon

Thursday, October 4, 2012

Abstraction and Encapsulation



        Before you were leaned what are the classes and object. Now we discuss additional information about Object Oriented Concepts. 
   
        There are few basic concepts underling the object oriented programming.

    * Abstraction
   * Encapsulation
   * Inheritance
   * Polymorphism

   Each discussion focuses on how these concepts relate to the real world. In this lesson we discuss about the Abstraction and Encapsulation.  



Abstraction




    Process of defining a data type, often call abstract data type. This data type refers to, providing only essential information to the outside word and hiding their background details and abstract is a design concept on which we only declare functionality doesn't define it because we don't know about them at design point.

    We can implement abstract is using either class or interface.



An abstract class-
    
    * Defined to be extend sub classes.
    * abstract keyword is used for definition

     
    abstract class Car{
                     
                    abstract int speed(){}
                    abstract boolean break(){} 
                    abstract void handling(){}                 
                  
          }

Encapsulation



   Encapsulation is a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Encapsulation is achieved by using modifiers. Modifiers are the keywords, that determine of a Class, Method or Variable. 

   
   1) Access modifiers- * Private
                        * Public
                        * Protected 
                        * Default
   
   2) Static modifiers

   3) Abstract modifiers

   4) Final modifiers

   5) Synchronized modifiers

   6) Volatile modifiers

   7) Native modifiers

 
  
    
Private modifiers-

Example- 


       private class School{
   
            private int stydentNo;
            private void learning(){}
       }


  class, variable and method are modified using private keyword. It's mean this class can't be access other classes, even sub classes. This class variables and methods are can only accessible by the class they are defined in. 



Public modifiers-


Example-


       public class Car(){
       
           public float temp;
           public void speed(){}
       } 


   This class is liked transparent. Anyone can access this class variables and methods. This class can be accessed by any other object. 



Protected modifiers-  


Example-


       protected class Tree(){
   
          protected int age;        
          protected boolean health(){

            return illness; 
          } 
       } 


    This means that protected class variables and methods have visibility only limited to subclass.  


Default modifiers-


Example:-

         class Man{
            int age;
            void running(){}
            void sleeping(){}             
         }


   There are no actual keywords used for this example for modify.
We call for it default modifier. 



Static modifiers- 


Example-


      class Cal{

         static int value;
         public static int sum(){}
      }



   Static modifiers do not use any instance variables of any object of the class they are defined in. It's take all the data from parameters and compute something from those parameters. When the variable is declared as once regardless of how many objects are instantiated.



Abstract modifier- 


Example-

       abstract class computer{

          abstract double voltage;
          private abstract double process(){}
       }
     
        
    
   One class can have create one or more abstract methods or variables, to declare the abstract classes variables or methods, use abstract keyword front of this. Abstract class can't be directly use new operator since their complete implementation is undefined. Abstract constructors or abstract static methods are not allowed in java programming. 



Final modifier- 


Example- 


      final int lastValue=64;


      When you are used final modifier for the methods or variables, It's cannot be override moreover.


  
Synchronized modifiers-


Example- 


    public synchronized class Car{}


    Used to specify that a method is thread safe. In a multi threaded environment like java,it's possible to have many different paths of execution running through the same code. 
If you have more than one of your threads running at the same time that access shared objects, it may cause your object to be in an inconsistent state.


Volatile modifiers- 

   

    Volatile modifier is used to the variables, It's can be changed unexpectedly by other parts of the program. May be modified by asynchronous threads.



Native modifiers-  


Example-


       native double apple();


    Native modifier have no body, It's totally deferrante other methods. The native modifier is never used with classes, constructors or variables.  
     


No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment