Recent

Powered by Blogger.

Services

Navigation-Menus (Do Not Edit Here!)

Popular Posts

Wednesday, October 19, 2016

Encapsulation in Java with example

I have already discussed a brief about encapsulation while discussing OOPs concepts. I have also covered polymorphism and inheritance in separate articles. In this post we will discuss encapsulation in java with examples. What is encapsulation? The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class. However if we setup public getter and setter methods to update (for e.g. void setSSN(int ssn))and read (for e.g. int getSSN()) the private data fields then the outside class can access those private data fields via public methods. This way data can only be accessed by public methods thus making the private fields and their implementation hidden for outside classes. That’s why encapsulation is known as data hiding. Lets see an example to understand this concept better. Published: By: Unknown - 9:13 PM

Constructors in Java – A complete study!!

Constructor is a block of code that allows you to create an object of class. This can also be called creating an instance. Constructor looks like a method but it’s not, for example methods can have any return type or no return type (considered as void) but constructors don’t have any return type not even void. There are several other differences between them, we will discuss them in detail at the end of this article. Types of Constructors There are three types of constructors: default, no-arg and parameterised. Default constructor: If you do not define any constructor in your class, java generates one for you by default. This constructor is known as default constructor. You would not find it in your source code but it would present there. It would look like this if you could see it. Default constructor for class Demo: Published: By: Unknown - 9:11 PM

Method Overloading in Java with examples

Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. In the last tutorial we discussed constructor overloading that allows a class to have more than one constructors having different argument lists. Argument lists could differ in – 1. Number of parameters. 2. Data type of parameters. 3. Sequence of Data type of parameters. Published: By: Unknown - 9:10 PM

Ads Inside Post

 

Like on Facebook