Posts

Showing posts from April, 2019

What is Encapsulation in Java?

Encapsulation is a mechanism of binding code and data together in a single unit. Let’s take an example of Capsule. Different powdered or liquid medicines are encapsulated inside a capsule. Likewise in encapsulation, all the methods and variables are wrapped together in a single class.


However if we setup public getter and setter methods to update (for example void setAge(int age))and read (for example  int getAge()) 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. Example of Encapsulation: /* Create a class Encapsulation.java */publicclassEncapsulation{privateString name;privateint age;publicint getAge(){return age;}publicString getName(){return name;}publicvoid setAge(int newAge){ age = new…

What is Inheritance in Java?

Inheritance is an important feature of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class.
Important terminology: Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
Syntax of Inheritance
class SubclassName extends SuperclassName  
{  
//methods and fields
}  

The extends keyword is used to create a new class that d…

Java OOPS Concepts - What is oops in Java?

Image
We used to hear OOPS very often whenever we come across JAVA language. In this post we are going to have a look at the OOPS concepts of the Java. Some of the object oriented languages are: C#, C++, PHP, Java, Python etc.

Main concepts of oops are:
ObjectClassInheritancePolymorphismAbstractionEncapsulationObject: A Java object is a combination of data and procedures working on the available data. An object has a state and behavior. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created from templates known as classes. In Java, an object is created using the keyword "new".
Syntax of creating an object:
MyClass obj = new MyClass()
In above line,  MyClass is class name, 'obj' is the object name (object name can be anything) 'new' is keyword of java which instantiates the object (or in simple language it creates the object'MyClass()'is the constructor of 'MyClass'
Class: B…

Guest Posts are invited on my blog!

Image
Hi Folks, You all are invited to share a guest post on my blog. If you are a techie and have quest to share your knowledge with other professionals, then this is the best opportunity. I will feature your post on my blog with your credentials. 

My blog has close to 4 lakh views. So, your post would be facing large audience. You are free to write blog on technical or non-technical thing,  like  technology, business, hr, science, finance, money, testing, development, travel, trips, technical events, nature, beauty, wildlife, life etc.

If interested you can let me know or share your content/post with me at vaneesh.behl@gmail.com.







Blog Author: Vaneesh Behl
Join Telegram channel of this blog at https://t.me/techlisticspace