Methods in Java

Methods in Java

1.What is method overriding?

A:Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its superclass. The key benefit of overriding is the ability to define behavior that’s specific to a particular subclass type.

Note:The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex: You can’t override a method marked public and make it protected).
You cannot override a method marked final
You cannot override a method marked static

2.Can overloaded methods be override too?

A:Yes, derived classes still can override the overloaded methods. Polymorphism can still happen. Compiler will not binding the method calls since it is overloaded, because it might be overridden now or in the future.

3.Is it possible to override the main method?

A:NO, because main is a static method. A static method can't be overridden in Java.

4.How to invoke a superclass version of an Overridden method?

A: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 the view of the subclass, the super prefix provides an explicit reference to the superclass' implementation of the method.
     // From subclass
5.What is super?

A:super is a keyword which is used to access the method or member variables from the superclass. If a method hides one of the member variables in its superclass, the method can refer to hidden variable through the use of the super keyword. In the same way, if a method overrides one of the methods in its superclass, the method can invoke the overridden method through the use of the super keyword.
Note:You can only go back one level.
In the constructor, if you use super(), it must be the very first code, and you cannot access any variables or methods to compute its parameters.

6.How do you prevent a method from being overridden?

A:To prevent a specific method from being overridden in a subclass, use the final modifier on the method declaration, which means "this is the final implementation of this method", the end of its inheritance hierarchy.
                           public final void exampleMethod() {
                          //  Method statements

7.What is an Interface?

A:An interface is a description of a set of methods that conforming implementing classes must have.

8.Can we instantiate an interface?

A:You can’t instantiate an interface directly, but you can instantiate a class that implements an interface.

9.Can we create an object for an interface?

A:Yes, it is always necessary to create an object implementation for an interface. Interfaces cannot be instantiated in their own right, so you must write a class that implements the interface and fulfill all the methods defined in it.

10.Do interfaces have member variables?

A:Interfaces may have member variables, but these are implicitly public, static, and final- in other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments for example.

11.What modifiers are allowed for methods in an Interface?

A:Only public and abstract modifiers are allowed for methods in interfaces.

12.What is a marker interface?

A:Marker interfaces are those which do not declare any required methods, but signify their compatibility with certain operations. The interface and Cloneable are typical marker interfaces. These do not contain any methods, but classes must implement this interface in order to be serialized and de-serialized.

13.What is an abstract class?

A:Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation.

14.Can we instantiate an abstract class?

A:An abstract class can never be instantiated. Its sole purpose is to be extended (subclassed).

15.When you declare a method as abstract, can other nonabstract methods access it?

A:Yes, other nonabstract methods can access a method that you declare as abstract.

16.Can there be an abstract class with no abstract methods in it?

A:Yes, there can be an abstract class without abstract methods.

17. Does the order of public and static declaration matter in main() method?

A:No. It doesn't matter but void should always come before main().

18.What are local variables?

A:Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them.

19.What are instance variables?

A:Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

20.. How to define a constant variable in Java?

A:The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed also.
static final int PI = 2.14; is an example for constant.

21. Should a main() method be compulsorily declared in all java classes?

A:No not required. main() method should be defined only if the source class is a java application.

16. What is the return type of the main() method?

A:Main() method doesn't return anything hence declared void.

22. Why is the main() method declared static?

A:main() method is called by the JVM even before the instantiation of the class hence it is declared as static.

23.Can a main() method be declared final?

A:Yes. Any inheriting class will not be able to have it's own default main() method.

24. What is the access scope of a protected method?

A:A protected method can be accessed by the classes within the same package or by the subclasses of the class in any package.

25. What is the arguement of main() method?

A:main() method accepts an array of String object as arguement.

26. What is the purpose of declaring a variable as final?

A:A final variable's value can't be changed. final variables should be initialized before using them.

27. What is the impact of declaring a method as final?

A:A method declared as final can't be overridden. A sub-class can't have the same method signature with a different implementation.

28.What is final keyword?

A:final keyword is used with Class to make sure no other class can extend it, for example String class is final and we can’t extend it.
We can use final keyword with methods to make sure child classes can’t override it.

29.What is static keyword?

A:static keyword can be used with class level variables to make it global i.e all the objects will share the same variable.
Static keyword can be used with methods also. A static method can access only static variables of class and invoke only static methods of the class.

30.What is finally and finalize in java?

A:finally block is used with try-catch to put the code that you want to get executed always, even if any exception is thrown by the try-catch block. finally block is mostly used to release resources created in the try block.

finalize() is a special method in Object class that we can override in our classes. This method get’s called by garbage collector when the object is getting garbage collected. This method is usually overridden to release system resources when object is garbage collected.

31.Can we declare a class as static?

A:We can’t declare a top-level class as static however an inner class can be declared as static. If inner class is declared as static, it’s called static nested class.
Static nested class is same as any other top-level class and is nested for only packaging convenience.

32.What is the importance of static variable?

A:static variables are class level variables where all objects of the class refer to the same variable. If one object changes the value then the change gets reflected in all the objects.

33. Can we declare a static variable inside a method?

A:Static varaibles are class level variables and they can't be declared inside a method. If declared, the class will not compile.


Post a Comment