Posts

Showing posts from October, 2020

Encapsulation in Java

Image
Encapsulation in java:- 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 class as private and writing public methods in the

Super keyword in java

Super Keyword in Java:- The super keyword in java is a reference variable that is used to refer parent class objects.   The keyword “super” came into the picture with the concept of Inheritance    Use of super keyword is as followed :- 1. Use of super with variables:  2. Use of super with methods:  3. Use of Super with Constructors        1. Use of super with variables:    This scenario occurs when a derived class and base class has same data members. In that case there is a possibility of ambiguity for the JVM.  We can understand it more clearly using this code snippet    class Animal {   String color="white";   }   class Dog extends Animal {   String color="black";   void printColor() {   System.out.println(color);  System.out.println(super.color);//prints color of Animal class   }   }   class TestSuper1 {   public static void main(String args[]) {   Dog d=new Dog();   d.printColor();  

Difference between overloading and overriding in java

S.NO Method Overloading Method Overriding 1. Method overloading is a compile time polymorphism. Method overriding is a run time polymorphism. 2. It help to rise the readability of the program. While it is used to grant the specific implementation of the method which is already provided by its parent class or super class. 3. It is occur within the class. While it is performed in two classes with inheritance relationship. 4. Method overloading may or may not require inheritance. While method overriding always needs inheritance. 5. In this, methods must have same name and different signature. While in this, methods must have same name and same signature. 6. In method overloading, return type can or cannot be be same, but we must have to change the parameter. While in this, r

Data types in java

 Data types in Java Java has two categories of data: Primitive Data Type: such as boolean, char, int, short, byte, long, float and double Non-Primitive Data Type or Object Data type: such as String, Array, etc. Primitive Data Type Primitive data are only single values and have no special capabilities.    There are 8 primitive data types: boolean: boolean data type represents only one bit of information either true or false, but the size of boolean data type is virtual machine-dependent. Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code. Syntax: boolean booleanVar; Size: virtual machine dependent Values: true, false Default Value: false filter_none edit play_arrow