Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways. For example, lets say we have a class Animal that has a method sound(). Since this is a generic class so we can’t give it a implementation like: Roar, Meow, Oink etc. We had to give a …
Continue reading Polymorphism in JavaPolymorphism in Java
Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways. For example, lets say we have a class Animal that has a method sound(). Since this is a generic class so we can’t give it a implementation like: Roar, Meow, Oink etc. We had to give a generic message.
public class Animal{
...
public void sound(){
System.out.println("Animal is making a sound");
}
}
Now lets say we two subclasses of Animal class: Horse and Cat that extends (see Inheritance) Animal class. We can provide the implementation to the same method like this:
public class Horse extends Animal{
...
@Override
public void sound(){
System.out.println("Neigh");
}
}
and
public class Cat extends Animal{
...
@Override
public void sound(){
System.out.println("Meow");
}
}
As you can see that although we had the common action for all subclasses sound() but there were different ways to do the same action.
What is polymorphism in programming?
Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations. As we have seen in the above example that we have defined the method sound() and have the multiple implementations of it in the different-2 sub classes.
Which sound() method will be called is determined at runtime so the example we gave above is a runtime polymorphism example.
Types of polymorphism and method overloading & overriding are covered in the separate tutorials. You can refer them here:
1. Method Overloading in Java – This is an example of compile time (or static polymorphism)
2. Method Overriding in Java – This is an example of runtime time (or dynamic polymorphism)
Static and dynamic binding in java
Association of method call to the method body is known as binding. There are two types of binding: Static Binding that happens at compile time and Dynamic Binding that happens at runtime. Before I explain static and dynamic binding in java,
What is reference and object?
class Human{
....
}
class Boy extends Human{
public static void main( String args[]) {
/*This statement simply creates an object of class
*Boy and assigns a reference of Boy to it*/
Boy obj1 = new Boy();
/* Since Boy extends Human class. The object creation
* can be done in this way. Parent class reference
* can have child class reference assigned to it
*/
Human obj2 = new Boy();
}
}
Static and Dynamic Binding in Java
As mentioned above, association of method definition to the method call is known as binding. There are two types of binding: Static binding and dynamic binding. Lets discuss them.
Static Binding or Early Binding
The binding which can be resolved at compile time by compiler is known as static or early binding. The binding of static, private and final methods is compile-time.
Static binding example
Here we have two classes Human and Boy. Both the classes have same method walk() but the method is static, which means it cannot be overriden so even though I have used the object of Boy class while creating object obj, the parent class method is called by it. Because the reference is of Human type (parent class). So whenever a binding of static, private and final methods happen, type of the class is determined by the compiler at compile time and the binding happens then and there.
class Human{
public static void walk()
{
System.out.println("Human walks");
}
}
class Boy extends Human{
public static void walk(){
System.out.println("Boy walks");
}
public static void main( String args[]) {
/* Reference is of Human type and object is
* Boy type
*/
Human obj = new Boy();
/* Reference is of HUman type and object is
* of Human type.
*/
Human obj2 = new Human();
obj.walk();
obj2.walk();
}
}
Dynamic Binding or Late Binding
When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding. Method Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method and in this case the type of the object determines which method is to be executed. The type of object is determined at the run time so this is known as dynamic binding.
The process by which one class acquires the properties(data members) and functionalities(methods) of another class is called inheritance. The aim of inheritance is to provide the reusability of code so that a class has to write only the unique features and rest of the common properties and functionalities can be extended from the another class. …
Continue reading Inheritance in Javaabstract class which is used for achieving partial abstraction. Unlike abstract class an interface is used for full abstraction. Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user Interface looks like a class but it is not a class. An interface can have methods …
Continue reading Interface in java with example programsTestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration. Introduction of TestNG TestNG, where NG stands for “next generation” is a test automation framework inspired by JUnit …
Continue reading TestNG TutorialsWhy Use Maven? Some of the key reasons Maven is used are: It simplifies the build process and provides a uniform system It handles compilation, distribution, dependency management and other tasks efficiently. It increases reusability. It reduces steps, like adding jar files to the project library, building reports, executing JUnit test cases, creating jar/war/ear files …
Continue reading Maven TutorialWhat Is Plain Old Java Object (POJO) ? – Full Stack QA (viralqa.com) How To Create POJO Classes Of A JSON Object Payload – Full Stack QA (viralqa.com) How To Create POJO Classes Of A JSON Array Payload – Full Stack QA (viralqa.com) How To Create POJO Classes Of A Nested JSON Payload – Full …
Continue reading POJOAPI Testing Overview Http methods for RestFul services Overview about RestAssured Setup a Basic REST Assured Maven Project Reason for Static Import In RestAssured Write First GET REST Assured Test Write First POST Request In REST Assured Write First PUT Request In REST Assured Write First DELETE Request In REST Assured Write API Response In …
Continue reading RestAssured AutomationIntroduction To JSON Creating JSON Object Request Body Using Java Map Creating JSON Array Request Body Using List How To Create A JSON Object Using Jackson API – ObjectMapper – CreateObjectNode() How To Use Java Object As Payload For API Request How To Create JSON Array Using Jackson API – ObjectMapper – CreateArrayNode()
Continue reading JSON ManupulationMost commonly used HTTP methods are GET, POST, PUT, PATCH and DELETE HTTP POST This method is used to Create new resources on server. normally Http 201 (Created) response is returned by server along with the ID of the newly created resource. Server may return 409 (Conflict), if the resource already exists or 404 (Not …
Continue reading Http methods for RestFul servicesLet us say that we need to get the weather data for my city today. To do this I will need to ask someone who knows about the weather conditions in my city. Assuming that computers are not yet available, we would typically look at the day’s newspaper or may be listen to the radio. …
Continue reading Client Server Architecture