Posts

Encapsulation in Java

Image
What is encapsulation? Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Other way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield. Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared. As in encapsulation, the data in a class is hidden from other classes using the data hiding concept which is achieved by making the members or methods of class as private and the class is exposed to the end user or the world without providing any details behind implementation using the abstraction concept, so it is also known as combination of data-hiding and abstraction. . Encapsulation can be achieved by: Declaring all the variables in the clas

Access Modifier in Java

Image
Access Modifier What is  Access Modifier? The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.                                                                      There are four types of Java access modifiers:       1.      Private       2.      Default       3.      Protected       4.      Public 1. Private : The access level of a private modifier is only  within the  class. It cannot be accessed from outside the class. Note: The scope of private modifier is limited to the class only -> Private Data members and methods are only accessible within the class                                                           -> Class and Interface cannot be declared as private -> If a class has private constructor then you cannot create the object  of that class from outside of the class. 2.   Default : The access le

Inheritance in Java

Image
   Inheritance of Java The process by which one class acquires the properties(data members) and functionalities(methods) of another class is called  inheritance .  Child Class: The class that extends the features of another class is known as child class, sub class or derived class. Parent Class: The class whose properties and functionalities are used(inherited) by another class is known as parent class, super class or Base class. Syntax: Inheritance in Java class Animal {      eat() method      sleep() method } class Dog extends Animal {      bark() method } In Java, we use the  extends  keyword to inherit from a class. Here, we have inherited the  Dog  class from the  Animal  class. The  Animal  is the superclass (parent class or base class), and the  Dog  is a subclass (child class or derived class). The subclass inherits the fields and methods of the superclass. Note: The derived class inherits all the members and methods that are declared as public or prot