Last Updated on July 9, 2023 by KnownSense
11. What is Encapsulation?
It is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object. We can make the fields as private or protected and let them to be used via Setters and Getters method by other classes.
12. What is Polymorphism?
Polymorphism means many forms. A single object can refer to the super-class or sub-class depending on the reference type which is called polymorphism.
Public class Bodmas(){ //Super class
public void add(){
}
}
public class Addition extends Bodmas(){ // Sub class
public void add(){
}
public static void main(String args[]){
Bodmas addition = new Addition(); //Bodmas is reference type and Addition is reference type
addition.add();
}
}
Using the Manipulation reference type we can call the Addition class “add()” method. This ability is known as Polymorphism. Polymorphism is applicable for overriding and not for overloading.
13. What is method overriding?
Method overriding happens if the sub-class method satisfies the below conditions with the Super-class method:
– Method name should be the same
– The argument should be the same
– Return type should also be the same
The key benefit of overriding is that the Sub-class can provide some specific information about that sub-class type than the super-class.
public class Bodmas{ //Super class
public void add(){
………………
}
}
Public class Addition extends Bodmas(){
Public void add(){
………..
}
Public static void main(String args[]){
Bodmas addition = new Addition(); //Polymorphism is applied
addition.add(); // It calls the Sub class add() method
}
}
addition.add() method calls the add() method in the Sub-class and not the parent class. So it overrides the Super-class method and is known as Method Overriding.
14.What is method overloading?
Method overloading happens for different classes or within the same class.
For method overloading, sub-class method should satisfy the below conditions with the Super-class method (or) methods in the same class itself:
– Same method name
– Different argument types
– There may be different return types
public class Bodmas{ //Super class
public void add(String name){ //String parameter
………………
}
}
Public class Addition extends Bodmas(){
Public void add(){//No Parameter
………..
}
Public void add(int a){ //integer parameter
}
Public static void main(String args[]){
Addition addition = new Addition();
addition.add();
}
}
Here the add() method has different parameters in the Addition class is overloaded in the same class as with the super-class.
Note: Polymorphism is not applicable for method overloading.
Important Note:
– Overloaded methods MUST change the argument list.
– Overloaded methods CAN change the return type.
– Overloaded methods CAN change the access modifier.
– Overloaded methods CAN declare new or broader checked exceptions.
– A method can be overloaded in the same class or in a subclass.
15. What is an Interface?
Multiple inheritances cannot be achieved in java. To overcome this problem the Interface concept is introduced.
An interface is a template which has only method declarations and not the method implementation.
Public abstract interface InterfaceBodmas //Interface declaration
Public abstract void add();//method declaration
public abstract void subtract();
}
- All the methods in the interface are internally public abstract void.
- All the variables in the interface are internally public static final that is constants.
- Classes can implement the interface and not extends.
- The class which implements the interface should provide an implementation for all the methods declared in the interface.
public class Bodmas implements InterfaceBodmas { //Bodmas class uses the interface
Public void add(){
……………
}
Public void subtract(){
…………….
}
}
16. What is an Abstract Class?
We can create the Abstract class by using the “Abstract” keyword before the class name.
An abstract class can have both “Abstract” methods and “Non-abstract” methods that are a concrete class.
Abstract method:
The method which has only the declaration and not the implementation is called the abstract method and it has the keyword called “abstract”. Declarations ends with a semicolon
public abstract class Bodmas{
public abstract void add();//Abstract method declaration
Public void subtract(){
}
}
- An abstract class may have a non- abstract method also.
- The concrete Subclass which extends the Abstract class should provide the implementation for abstract methods.
17. What is Dynamic Binding?
Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.
18. Can overloaded methods be overridden?
Yes, derived classes still can override the overloaded methods. Polymorphism can still happen. The compiler will not be binding the method calls since it is overloaded, because it might be overridden now or in the future.
19. Can the main method be overridden in java?
No, because the main is a Static method. A Static method can’t be overridden in Java.
20. How to invoke superclass version of an overridden method?
To invoke a superclass method that has been overridden in a subclass, you must either call the method directly through a superclass instance or use the super prefix in the subclass itself. From the point of view of the subclass, the super prefix provides an explicit reference to the superclass’s implementation of the method.
// From subclass
super.overriddenMethod();
Authored by codingknownsense.com