Showing posts from July, 2019

Java Encapsulation - The Coding Shala

Home >> Learn Java >> Java Encapsulation Java Encapsulation Java Encapsulation is a process of wrapping code and data together into a single unit. We can say Encapsulation is a protective shield that prevents the data from being accessed by the code outside this shield, for example, a capsule. In encapsulation, the variables or data of a class are hidden from any other class and can be accessed only through any member function of its own class so it is also known as data-hiding.  We can achieve Java Encapsulation by declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. These public methods called getter and setter methods. Using these getter and setter we can make our class read-only or write-only. Using Encapsulation we can control the data. The encapsulation class is easy to test. The Java Bean class is the example of a fully encapsulated class. Example of Java Encapsulat

Java Interfaces - The Coding Shala

Home >> Learn Java >> Java Interfaces Java Interfaces Java Interfaces are used to achieve Abstraction. Interfaces are like abstract class but the interface only contains an abstract method. Every method declared in an interface are by default abstract. We can see the interface as the blueprint of the class. It specifies what a class must do and not how. We use the interface keyword to declare interfaces. To implement the interface we use implements keyword.  The following code shows how to use java interfaces:  interface interface_name { //declare constant fields //abstract methods //by default } class class_name implements interface_name { //class body //implementation of all the abstract methods } The following are the important point about Java interfaces:  Java Interfaces provide total abstraction. In java interfaces all the methods are abstract and all fields are public, static and final by default.  If a clas

Java Abstract Class - The Coding Shala

Home >> Learn Java >> Java Abstract Class Abstraction in Java Abstraction is an important pillar of object-oriented programming language. Abstraction is a process of hiding the implementation details and only showing the functionality to the user. Let's take an example Car. A user only knows how to drive, how to use breaks, how to change gears not all the details of how breaks work and how gear works. In Java when we know what object does not how the object does it, is called Abstraction. There are two ways to achieve abstraction in Java: Abstract class Interface In this chapter, we will see the Abstract class. Java Abstract class A class that is declared as abstract is known as an abstract class. We use the abstract keyword to declare an abstract class. An abstract class can have an abstract and non-abstract method. Abstract classes have partial implementation. We have to extend abstract classes to subclass and implement all the abstract met

Java Static Binding and Dynamic Binding - The Coding Shala

Home >> Learn Java >> Java Static Binding and Dynamic Binding Java Static Binding and Dynamic Binding What do you mean by Binding? Binding means connecting a method call to the method body. Binding determines the type of object. There are two types of binding available in Java: Static Binding Dynamic Binding Java Static Binding If the type of object is determined at the compiled time by the compiler, it is known as Static Binding or Early Binding. Binding of all the static, private and final methods is done at compile time or called static binding. We know static, private and final methods can not be overridden and always accessed by an object of a local class. That's why the compiler can easily determine the object of class and binding is done at compile time. Example of Java Static Binding The following example explains Java Static Binding:  //Java Static Biniding Example // class Parent { static

Java Runtime Polymorphism - The Coding Shala

Home >> Learn Java >> Java Runtime Polymorphism Java Runtime Polymorphism Polymorphism means many forms. There are two types of polymorphism in Java: compile-time polymorphism Runtime polymorphism Compile-time polymorphism is achieved by overloading and Runtime polymorphism is achieved by method overriding. We will see Runtime polymorphism in this chapter. Runtime polymorphism is also known as Dynamic Method Dispatch. Runtime polymorphism is a process in which a call to an overridden method is resolved at runtime, not at the compile-time. An overridden method is called through the reference variable of a superclass. We use upcasting for runtime polymorphism. Upcasting If the reference variable of Parent class refers to the object of Child class, it is known as upcasting. The following code shows how to achieved runtime polymorphism using upcasting.  class Parent {} class Child extends Parent {} Parent p = new Child ();

Java Covariant Return Types - The Coding Shala

Home >> Learn Java >> Java Covariant Return Types Java Covariant Return Types In Java 5.0 onward it is possible to override a method by changing the return type. The child's return type should be sub-type of parent's return type. Covariant return type refers to the return type of an overriding method. It works only for non-primitive return types. Example of Covariant Return Type in Java The following is a simple example of the covariant return type is java:  //covariant return type example // class Parent { Parent Display (){ System . out . println ( "Inside Parent's Display" ); return this ; } } class Child extends Parent { Child Display (){ System . out . println ( "Inside Child's Display" ); //can write also //return this; return new Child (); } } class Main { public static void main ( Stri