Exam Flashcards
Switch
A switch works with the byte , short , char , and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character , Byte , Short , and Integer (discussed in Numbers and Strings
Inner class
static nested class can access static member of enclosing class
member inner class can access static member of enclosing class
member inner class can access instance members of the enclosing class
local class can access instance members from within an instance method
anonymous class can access instance members from within an instance method
Short
you can assign any int literal that falls in the range of short (-32768 to 32767) to a short variable.
boolean variable cannot be cast to any other data type and vice versa
overriding
Overriding and Access-Modifiers : The access modifier for an overriding method can allow more, but not less, access than the overridden method.
Final methods can not
be overridden :
Static methods can not be overridden
Private methods can not be overridden
The overriding method must have same return type (or subtype)
Overriding and constructor : We can not override constructor as parent and child class can never have constructor with same name
If the super-class overridden method does not throw an exception, subclass overriding method can only throws the unchecked exception, throwing checked exception will lead to compile-time error.
Rule#2 : If the super-class overridden method does throws an exception, subclass overriding method can only throw same, subclass exception. .Also there is no issue if subclass overridden method is not throwing any exception
Overloading
methods must have same name and different signature.
return type can or can not be be same, but we must have to change the parameter.
Enum and static
Only top level types and static nested classes can define static members other than static constant. Emum are implicitly static
Static method can not be overridden
Anonymous class can not be abstract or final since it has not class definition
Only static nested class can include static method
local variable type inference
Not permitted in class fields Not permitted for uninitialized local variables Not allowed as parameter for any methods Not permitted in method return type Not permitted with variable initialized with ‘NULL'
@SafeVarargs Annotation
Final methods Static methods Constructors From Java 9, it can also be use Private Instance Method
nested inner class and nested static inner class
To create an instance of nested inner static class OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
To create an instance of nested inner class OuterClass outerObject = new OuterClass() OuterClass.InnerClass innerObject = outerObject.new InnerClass();
local inner class instance is tied to and can access the final local variables of its containing method.