↪Yes, we can overload the main method in Java. Overloading allows a class to have multiple methods with the same name but different parameter lists. However, only the standard public static void main(String[] args) method is the entry point of the Java program.
↪In Java, a constructor doesn't return a value explicitly. The purpose of a constructor is to initialize the object and allocate memory for it. The return type for a constructor is implicitly the type of the object being constructed.
Q. Can we create a program without the main method?
↪No, a Java program must have a public static void main(String[] args) method as the entry point. Without the main method, the Java Virtual Machine (JVM) won't know where to start the execution.
Q. What are the six ways to use the this keyword?↪The this keyword in Java is used in the following six ways:To differentiate instance variables from parameters.
- To invoke the current class method.
- To invoke the current class constructor.
- To pass as an argument in a method call.
- To return the current instance from a method.
- To pass the current instance to other methods.
Q. Why is multiple inheritance not supported in Java?
↪Java does not support multiple inheritance to avoid the "diamond problem," where the ambiguity arises if a class inherits from two classes with a common ancestor. To prevent confusion and maintain simplicity, Java supports multiple inheritance through interfaces.
Q. Why use aggregation?
↪Aggregation in Java is used to represent a "whole-part" relationship between two classes. It allows one class to contain another class as a member. Aggregation is used for code reusability, flexibility, and better organization of code.
Example:Q. Can we override the static method?
↪No, static methods in Java belong to the class rather than an instance, and they cannot be overridden in the traditional sense. However, if a subclass defines a static method with the same signature as a static method in the superclass, it is called method hiding, not overriding.
Q. What is the covariant return type?↪Covariant return type allows overriding a method in a subclass with a return type that is a subtype of the return type in the superclass. This feature was introduced in Java 5.
Example:Q. What are the three usages of Java super keyword?
↪The super keyword in Java is used for:Invoking the superclass method.
- Accessing the superclass fields.
- Invoking the superclass constructor.
Q. Why use instance initializer block?
↪Instance initializer blocks are used to initialize instance variables and are executed each time an object of the class is created. They are useful when complex initialization is required and can be an alternative to constructors.
Example:Q. What is the usage of a blank final variable?
↪A blank final variable is a final variable that is not assigned a value during declaration. Its value must be assigned in the constructor or an instance initializer block and cannot be changed afterward.
Example:Q. What is a marker or tagged interface?
↪A marker or tagged interface is an interface with no methods. It is used to tag or mark classes that implement it, providing metadata or indicating certain behavior.
Example:Q. What is runtime polymorphism or dynamic method dispatch?
↪Runtime polymorphism, also known as dynamic method dispatch, is a feature where the method called is determined at runtime rather than compile time. It is achieved through method overriding.
Example:Q. What is the difference between static and dynamic binding?
- Static Binding: The method called is determined at compile time. It is also known as early binding.
- Dynamic Binding: The method called is determined at runtime based on the actual object. It is also known as late binding or runtime polymorphism.
Q. How is downcasting possible in Java?
↪Downcasting in Java is possible using explicit casting, but it requires checking the type using the instanceof operator to avoid ClassCastException at runtime.
Example:Q. What is the purpose of a private constructor?
↪A private constructor is used to prevent the instantiation of a class from outside the class itself. It is often used in classes that contain only static methods or constants and should not be instantiated.
Example:Q. What is object cloning?
↪Object cloning is the process of creating an exact copy of an object. In Java, it can be achieved by implementing the Cloneable interface and overriding the clone method. Cloning can be either shallow or deep, depending on the object's structure.
Example:
Comments
Post a Comment