Java Interview questions:-
A: Declaring a class final is the simplest way to prevent extension. A more limited way to control overrides without preventing extension is to declare individual methods final. If all methods are declared final but not the class, the class may be extended but none of its methods overridden.
A: The final keyword is a modifier that can be applied to classes, methods and variables to control how they are used in a Java program. Final classes cannot be extended, final methods cannot be overridden, final variables can only have a value assigned once and not modified. For example, if you declare a method argument as final, the object reference passed into the method cannot be assigned a different reference. A final method implementation cannot be changed by subclasses.
public final void doSomething(final Object argument) {
// Not possible with a final argument
// argument = new Object();
// OK
System.out.println(argument. toString());
// Also OK
try {
Object clone = argument.clone();
}
catch (CloneNotSupportedException cnse) {
// Handle exception
}
}
An abstract class may be used to define a core set of methods for use by subclasses, and may also define abstract methods that act like a template for subclasses to implement. Abstract classes often represent a concept in a program that isn't a meaningful thing in its own right. For example, the concept of a "pedal vehicle with wheels" might be represented by an AbstractCycle class with concrete crank() andchangeGear (int) methods. It might also have an abstract Wheel[] getWheels() method that subclassesMonoCycle, BiCycle and TriCycle would implement by returning the appropriate number of wheels in the array.
Java visibility modifiers
A: The Java visibility modifiers are firstly concerned with which classes can access the variables and methods, and secondly with inheritance. If a field or method is marked public or protected it will be accessible in sub-classes too. If it has no explicit visibility modifier, it has implicit package visibility status, which is accessible to sub-classes in the same package, but not to sub-classes in a different package.
The private visibility keyword means only accessible from the host class, so will not be accessible in any subclass. The public means any class can read a variable or call a method.
A: The Java visibility modifiers are concerned with data hiding and encapsulation, they control which classes and sub-classes can read and write variable values and call methods. The protected visibility modifier means that the field or method is accessible from within the host class itself, and internally to other classes in the same package and subclasses that may be in a different package. Protected fields cannot be accessed through the public API of a class.
The protected modifier is usually used to develop the features of an inheritance hierarchy by sharing access to common variables and methods with subclasses. It offers greater scope for extension than packagevisibility without creating a public API.
A: The use of the protected modifier on a superclass method means that any subclass can call that method on the superclass through itself. Such calls are made with an implied "this" reference with the same syntax as a call to one of their own instance methods, they do not require an explicit super. prefix.
int example = protectedGetInt();
In this respect protected method inheritance is the same as public method inheritance, but the protectedmodifier means that the method is not part of the class' public API; other classes cannot call the method on an instance of the class. Typically another class may call a public method that internally calls the protected method to complete the request.
A: With this type of question it is often easiest to create a minimal test case and attempt to compile it. In this case you will find that the compiler will fail and issue a warning if you attempt to assign "weaker" access privileges to the overridden methods using private or implicit package private modifiers. The term weaker is slightly misleading in this context; it means that the private and package access modifiers are more restrictive than the parent classes' method, which is not permitted. In short, only protected or publicvisibi lity modifiers can be used to override protected methods.
A: Yes, Java class constructors are assigned access modifiers. If none is explicitly declared, the constructor will implicitly have package visibility, which means it is accessible by the class itself and other classes in the same package only. Constructors may also be assigned private, protected or public access. The choice of constructor visibility modifiers will depend on the nature of your application, but the notes below give some broad principles in each case.
No comments:
Post a Comment