Wednesday 2 October 2013

Abstraction and Encapsulation in Java



Java is a language of objects. Different objects created by different people are used by different people at different places, there by creating an application for a given purpose. To do so, java has provided us with different types of classes, methods and variables to deal with. One of them is abstraction that helps java programmers to deal with the objects in the case when they are more generalized or not real and thus are not required to be instantiated. There can be several reasons though, that some of the classes should not be instantiated, and some of the classes need not be instantiated. Some examples that will help us understand this is that some things can not exist; they are just unreal or imaginary. For e.g. good and bad, they all are feelings and can only be imagined. Therefore these should be declared as abstract class. And then there are some things that are much generalized, the common nouns, - like a book. And thus they need to be made abstract class.
It did take a lot of time for me to understand about which classes should be abstract and which should not be. Now if I consider that class Good can be abstract and I declare it as abstract, but it would be of no use if I don’t use them. Abstract classes in them are not at all of any use as there is no implementation of that class. An abstract class has methods that are declared as abstract or even not. All the methods that are declared as abstract have only signatures and they are without body. To utilize them, we need to use it in non abstract class and give them body that is writing the implementation. All the non-abstract methods in abstract class already have the implementation, which can be used in normal classes.
It would be like a class Good if not used on any person, or animal or thing, then Good will just remain a feeling. So I should say Good boy, or good girl. For that I will have to use that abstract class in some concrete class. So now I declare another normal class ‘Boy’ and use ‘Good’ abstract class. Once when I use Good class in Boy subclass, I can use all the methods written in Good class in Boy class to give it a proper implementation. For using both these classes and the methods written in both the classes, I have created another class called StudentConduct.
public abstract class Good {
private String quality;
public abstract void getDegreeOfGoodness(int goodnessMarks);
public String getQuality()
{
quality = "Good";
return quality;
}
}

public class Boy extends Good {
int behaviorMarks=0;
String degree="";
public void getDegreeOfGoodness(int behaviorMarks)
{
if(behaviorMarks >10)
degree="Very Very";
else if((behaviorMarks <> 7))
degree = "Very";
else if((behaviorMarks <> 4))
degree = "Ok";
else if(behaviorMarks < 4)
degree = "Not";
System.out.println(degree+" "+getQuality());
}
}

public class StudentConduct extends Boy {
public void getStudentConduct()
{
Boy b1 = new Boy();
b1.getDegreeOfGoodness(5);
}
public static void main(String args[])
{
StudentConduct sc = new StudentConduct();
sc.getStudentConduct();
}
}

The above abstract class Good, has two methods, one is abstract and another one, non-abstract, returning the string quality. The method is “getQuality()”, that has a string ‘quality’. In another class, the method is directly called, without changing the string ‘quality’ there by making sure it is untouched. Also the string is declared as private, to protect it from getting changed in any other place. Once this string is declared as private, its value cannot be changed. This phenomenon is called encapsulation or hiding and protecting your data from misuse.

2 comments:

  1. A good blog always contains new and exciting information and as I read it I felt that this blog really has all of these qualities that make a blog. We are also providing the best services click on below links to visit our website.
    Oracle Fusion HCM Training
    Workday Training
    Okta Training
    Palo Alto Training
    Adobe Analytics Training

    ReplyDelete