What is run-time polymorphism and how it is achieved in Java?
Run-time polymorphism(dynamic binding or dynamic method dispatch) implies that the call to an overridden method is resolved dynamically during run-time instead of compile-time.
Run-time polymorphism is achieved with the help of method overriding in Java. When a child class(subclass) has the same method name, return type, and parameters as the parent(superclass), then that method overrides the superclass method, this process is known as method overriding.Example: The below example has one superclass Animal and three subclasses, Birds, Mammals, and Reptiles. Subclasses extend the superclass and override its print() method. We will call the print() method with the help of the reference variable of Animal class i.e., parent class. The subclass method is invoked during runtime since it is referring to the object of the subclass and the subclass method overrides the superclass method. As Java Virtual Machine(JVM) decides method invocation, it is run-time polymorphism.
Output:
What is the significance of the “super” and “this” keywords in Java?
super keyword: In Java, the “super” keyword is used to provide reference to the instance of the parent class(superclass). Since it is a reserved keyword in Java, it cannot be used as an identifier. This keyword can also be used to invoke parent class members like constructors and methods.
this Keyword: In Java, the “this” keyword is used to refer to the instance of the current class. Since it is a reserved keyword in Java, it cannot be used as an identifier. It can be used for referring object of the current class, to invoke a constructor of the current class, to pass as an argument in the method call or constructor call, to return the object of the current class.
Accenture Technical Interview Questions: Freshers and Experienced
The static keyword is a non-access modifier in Java that is useful for memory management.
Static property can be shared by all the objects, no separate copies of static members will be created on object creation.
No need to create the instance of the class for accessing static members, we can directly access them by using the class name.
The static keyword can be used with the variable, block, method, and nested classes for memory management.
Static variable: When a variable is declared with the static keyword, a single copy of the variable will be created and the same variable will be shared across all objects of the class (a class to which the static variable belongs).
Static block: A static block helps with the initialization of the static data members. It is a group of statements within a Java class and gets executed exactly once when the class is first loaded into the JVM(Java Virtual Machine).
Static method: If the method is declared with the static keyword, then it is considered a static method. The main( ) method is one of the examples of a static method. Static methods are having restrictions such as they can directly call other static methods only, and they can access static data directly.
Static class: Only a nested class can be created as a static class. Nested static class doesn’t need a reference of Outer class(a class in which the nested class is defined). A static class does not have permission to access non-static members of the Outer class.
1 Explain about getch() function in a C++ program. How it is different from getche() function?
The getch() is a pre-defined library function in C++ that is used to receive a single input character from the keyboard, and it holds the screen until it does not receive any character from standard input. This function does not require any arguments and it is defined under the “conio.h” header file.
This program holds the output screen until you press any character on the keyboard.
The only difference between these two functions is getch() does not echo the character to the screen whereas getche() does.