More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. static is the keyword that makes it accessible even without creating any object and using class name only. Making it non static would like creation of object upfront before calling the method.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes we can have a main method with string[] argument in every class of an application. When we execute an app we specify the starting point i.e the class that will get the control first and hence main method of that class gets executed.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Abstract methods are the methods that need to be overridden in the derived class ( either as implementing method or again abstract method ) so it's not only allowed but its required to override abstract method in the derived class.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, but in that case the derived class itself should be abstract. We cannot have an object of a class without definition for the method which has been declared abstract in the parent.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Though it's useful but it's not as useful as overriding member or object methods. We cannot achieve polymorphic behavior with static methods by overriding their definition in derived class.
Help us improve. Please let us know the company, where you were asked this question :
Q79. If we don't provide any argument to the executed program, What arguments will be passed to the main method ? Will the String array argument be null ?
Ans. it depends on the implementation of equals method of the respective class. If no definition is provided and it uses the default definition of the object class, two references are equal only if they point to the same object.
We can have such an equality defined for a particular class objects if we provide appropriate implementation of equals method comparing all those fields.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Public constructor is simple and easy as it's the default way of object creation. So there are no additional coding overheads as compiler provides the default constructor if none is provided by coder.
With static final methods, it facilitates loose coupling by segregating the responsibility of object creation to a separate method. Validation can be done on the constructor arguments before calling it. Moreover if any adaption on the arguments is required that can achieved easily with factory method.On the flip side, there is coding overhead and additional method call.
Ans. All methods are not expected to return something but Yes, all methods are expected to have a return type. If a method returns nothing, it can be declared with the return type void.
Constructors are not expected to have any return types , not even void.
Help us improve. Please let us know the company, where you were asked this question :
Ans. BuggyBread method without any return type is the constructor which get's called upon object creation whereas BuggyBread method with return type of void is just another method that needs to be called explicitly for it's invocation.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Var args is the feature of Java that allows a method to have variable number of arguments. Var args are used when we are not sure about the number of arguments a method may need. Internally java treats them as array.
Ans. Yes, it will give compilation error and java will complain about duplicate method. Java treat var args internally as arrays and hence would result in same byte code for both method's syntax.
Help us improve. Please let us know the company, where you were asked this question :
Q97. What are the advantage of Abstract classes over interfaces with respect to Java 7 ? and What changed in Java 8 to help facilitate that in Java 8 ?
Ans. Abstract Classes provide default implementations of methods that are inherited by the classes that extend them, which was not the case for Interfaces. This changed in Java 8, where default implementations are provided for methods.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Java 8 allowed for method implementation using default methods in interfaces. As those default methods could contain complex logic and might need organizing the logic into multiple methods, they have allowed for private methods.
Help us improve. Please let us know the company, where you were asked this question :