Sunday, November 27, 2016

encapsulation define - Abstraction & Encapsulation

   Use  Abstraction Classes & Encapsulation, Good idea


In general term the abstraction means dealing with ideas rather than events, now the same concepts are also with in programming that abstraction means hiding the implementation details or internal process just showing functionality to the users is known as abstraction. Take an example of phone call, you just dialed number and call your friends, this is what? This is functionality of the phone but you are not aware with internal process.When we have to do something but we are sure that how the will looks like? In such cases we have to use abstraction.Abstraction & Encapsulation

There are two ways to achieve abstraction in Java
o   By Abstract Class
o   By Interface

Abstraction & Encapsulation

            Abstract Class
In java abstract is a keyword if any class declared as an abstract is known as abstract class. through Abstract class we will get partial abstraction (0 to 100%). You may create methods, constructors, data members inside of the Abstract class.

If a method is created as an abstract and does not having implementation is known as Abstract Method. If you declare an Abstract method than that class must be declare as an abstract otherwise, you will get a compile time error. These abstract methods are helps to achieve abstraction because these methods just have method declaration not implementation details. So, as it is declared once than its up to us that how we can implement that abstract methods.Abstraction & Encapsulation

Example with real world


Abstract Class

In above example we create and abstract class “Car” with abstract method “run ()”. Now the other class that inherit the abstract class or become the child of abstract class they implement the abstract method according to their nature. So this process is achieving the abstraction.Abstraction & Encapsulation

Example in term of constructor and methods

abstractMethod
Abstraction & Encapsulation

         Interface in Java

Interface is way to achieve fully abstraction (100%) in java. Interface are used to achieve 100% abstraction and multiple inheritance. Interface contain only abstract methods data members inside it that’s why interface achieves the 100% abstraction.

In interface the methods are public and abstract by default and the data members are public, static and final by default. If you don’t mention public and abstract with methods and public, static and final with data members than JVM automatically includes these keywords for you.Abstraction & Encapsulation

In java when we come across the concept of child and parent then we say its inheritance so to achieve inheritance we use extends keyword. But for interface we use implements keyword to achieve inheritance. Understand the below relationship between class and interface.
                Class extends another Class
                Class implements interface1, interface2, …..
                Interface extends another interface.

Example with multiple inheritance

multipuleInhirtance

Now above example is showing multiple inheritance.

  Note: for interface you no need to create an object.

The interface without member is known as marked and tagged interface. Examples: Serializable, Cloneable, Remote, etc.

Abstraction & Encapsulation

         Encapsulation

Encapsulation is the fourth and last pillar of OOP. Encapsulation is a process of wrapper different type of data and code into a single unit or package. To make your class fully encapsulated than makes all data members private and create setter and getter method to set and get the value of respected data members. POJO and Java Bean are the best examples of encapsulation. Singleton pattern make good use of encapsulated class.
Abstraction & Encapsulation

Example:

Encapsulation
Abstraction & Encapsulation
Now above is the example of Encapsulated class, in main method we create an object of encapsulated class then through object reference we first set the value and then get the value.Abstraction & Encapsulation







No comments:

Post a Comment