<-Pre | Home | Next-> |
A method declared as final can't be overridden. A sub-class doesn't have the independence to provide different implementation to the method.
Can a final variable be declared inside a method?
Yes.
I don't want my class to be inherited by any other class. What should i do?
You should declared your class as final. A class declared as final can't be inherited by any other class.
When will you declare a class as final?
When a class is independent and does not required for inheritence.
Can you give few examples of final classes defined in Java API?
java.lang.String,java.lang.Math are final classes.
How to define a constant variable in Java?
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 e = 2.71; is an example for constant.
Can a class be declared as static?
No a class cannot be defined as static. Only a method,a variable or a block of code can be declared as static.
When will you define a method as static?
When a method needs to be accessed even before the creation of the object of the class then we should declare the method as static. (ex. main method.)
I want to print "Hello" even before main is executed. How will you acheive that?
Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main method.
What is the use of a static code block?
static code blocks could be used for one time initialisation activities.
Cant you use the constructor for initialisation rather than static block?
Constructors are used for object level initialisation whereas the static block are used for class level initialisation ie to initialise constants.
Will the static block be executed for each object?
No. It will be executed only once for each class ie at the time of loading a class.
What are the restriction imposed on a static method or a static block of code?
It cannot use "this" or "super". A static method can acces only static variables or static methods.
When overriding a static method, can it be converted to a non-static method?
No. It should be static only.
What is the importance of static variable? *IMP*
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.
Is it possible to declare a static variable inside a method *IMP*
No. Static varaibles are class level variables and they can't be declared inside a method. If declared, the class will not compile.
What is an Abstract Class? *IMP*
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.
Is it possible to decleare an abstract variable?
Variables can't be declared as abstract. only classes and methods can be declared as abstract.
Is it possible for an abstract class be declared final?
Not possible. An abstract class without being inherited is of no use and a final class cannot be inherited.
What is an abstract method?
An abstract method is a method which define into a subclass..What happens if a class has implemented an interface but has not provided implementation for a method in a interface?
Its the same as the earlier answer. The class has to be marked as abstract. This will be enforced by the compiler.
What is the purpose of transient modifier?
Only variables can be marked as transient. Variables marked as transient will not be persisted during object persistence.
What is the purpose of volatile modifier?
Only variables can be marked as volatile. Volatile variables might be modified asynchronously.
What is an Interface?
Interfaces say what a class must do but does not say how a class must do it. Interfaces are 100% abstract.
Class Test implements Interface Inter containing method a1 and a2 declarations. Class Test has provided implementation for method a2. Can i create an object of Class Test?
No. Class Test should provide implementation for all the methods in the Interface Inter.
Can a method inside a Interface be declared as final?
No. Doing so will result in compilation error. public and abstract are the only applicable modifiers for method declaration in an interface.
Can an Interface extend another Interface?
Yes. An Interface can inherit another Interface, for that matter an Interface can extend more than one Interface.
Can a Class extend more than one Class?
Not possible. A Class can extend only one class but can implement any number of Interfaces.
Why is an Interface be able to extend more than one Interface but a Class can't extend more than one Class? *IMP*
Basically Java doesn't allow multiple inheritance, so a Class is restricted to extend only one Class. But an Interface is a pure abstraction model and doesn't have inheritance hierarchy like classes(do remember that the base class of all classes is Object). So an Interface is allowed to extend more than one Interface.
Can an Interface be final?
No.
Can a class be defined inside an Interface?
Yes.
Can an Interface be defined inside a class?
Yes.
Can we define private and protected modifiers for variables in interfaces?
No. Always all variables declared inside a interface are of public access.
What modifiers are allowed for methods in an Interface?
Only public and abstract modifiers are allowed for methods in interfaces.
What are Inner Classes?
Inner classes are classes which are defined inside another class.
What are Nested Classes?
Static Inner classes are called sometimes referred as nested classes because these classes can exist without any relationship with the containing class.
What is the super class of all Inner Classes?
Inner class is just a concept and can be applied to any class, hence there is no common super class for inner classes.
What are the disadvantages of Inner classes?
1. Inner classes are not reusable hence defeats one of the fundamental feature of Java.
2. Highly confusing and difficult syntax which leads poor code maintainability.
Name the different types of Inner Classes?
The following are the different types of Inner classes:
Regular Inner Class
Method Local Inner Class
Static Inner Class
Anonymous Inner Class
What are Regular Inner Classes?
A Regular inner class is declared inside the curly braces of another class outside any method or other code block. This is the simplest form of inner classes.
Can a regular inner class access a private member of the enclosing class?
Yes. Since inner classes are treated as a member of the outer class they can access private members of the outer class.
How will you instantiate a regular inner class from outside the enclosing class?
Outer out=new Outer();
Outer.Inner in=out.new Inner();
What is the impact of marking an Inner Class as abstract?
Explain how "this" works with respect to inner classes?
Within the inner classes "this" refers to the current object but if an inner class needs to access an current outer class object, then it should outer class name along with this. The below is an example for the same.
<Program needs to be given>
What are Local Inner Classes or Method Local Inner Classes?
A method-local inner class is defined within a method of the enclosing class.
What are the constraints on Method Local Inner Classes?
The following are the restrictions for Method Inner Classes:
- Method Local Inner classes cannot acccess local variables but can access final variables.
- Only abstract and final modifiers can be applied to Method Local Inner classes
- Method Local Inner classes can be instantiated only within the method in which it is contained that too after the class definition.
What are Anonymous Inner Classes? Name the various forms of Anonymous Inner Classes.
Anonymous Inner Classes have no name, and their type must be either a subclass of the named type or an implementer of the named interface. The following are the different forms of inner classes:
- Anonymous subclass(i.e. extends a class)
- Anonymous implementer (i.e. implements an interface)
- Argument-Defined Anonymous Inner Classes
How many classes can an Anonymous Inner classes inherit from?
One.
How many Interfaces can an Anonymous Inner classes implement?
One. Normal classes and other inner classes can implement more than one interface whereas anonymous inner classes can either implement a single interface or extend a single class.
In which scenarios are Anonymous Inner classes frequently used?
What are Static Inner Classes?
Static Inner Classes are inner classes which marked with a static modifier. These classes need not have any relationship with the outer class. These can be instantiated even without the existence of the outer class object.
Can you instantiate the static Inner Class without the existence of the outer class object? If Yes, Write a sample statement.
Yes. It can be instantiated as follows by referencing the Outer class.
Outer.Inner in = new Outer.Inner();
What are the constraints on Static Inner Classes?
- It cannot access non-static members of the outer class.
- It cannot use this reference to the outer class.
On many class files are produced for source file having one Outer class and one Inner class?
Two class files will be produced as follows:
Outer.class
Outer$Inner.class
Previous |
*****Please feel free to give feedback at shrawan.iitg@gmail.com and comments below *****
No comments:
Post a Comment