Professional Documents
Culture Documents
5 Notes
1) Please say something about yourself?
4) Constructors
Q) Can I call an non static method in a constructor --- no, because the object construction is not yet
complete.
Q) Can I call a static method in a constructor – yes, because the static methods will be loaded when the
class loader loads the class and this happens even before the object is created.
Q) Can I call the constructor like a method in my static or non static methods – yes
Q) Can I call one constructor from another – yes, using the this() and either the this() or super() must be the
first call in the constructor.
Q) Does the jvm add a default constructor, when we have a parameterized constructor – no the jvm does
not add the default constructor when we have a parameterized constructor
Datatypes
The first bit is the sign bit (O represents positive, 1 represents negative)
So the remaining bits i.e. (n-1) would be the value bits.
Legal :-
void doStuff(int… x); //uses primitive type for var-args
void doStuff(char c, int… str); //valid to use other parameters along with the var-args
void doStuff(Animal… animal); //uses object type for var-args
Illegal :-
void doStuff(char… c, String… str); // cannot have more than one var-args parameter
void doStuff(char… c, String str); // the var-args parameter must be the last parameter in the signature
void doStuff(int x…)
Scenario :-
m1() {
doStuff(5, “String1”);
// any number of string objects can be passed here for the var-args
doStuff(5, “String1”, “String2”);
// any number of string objects can be passed here for the var-args
doStuff(5, new String[]{“String1”, “String2”});
doStuff(5); // not passing the string object(s)
}
3) Enums :-
*) Enums cannot be instantiated.
*) Enums can have constructors, but you cannot invoke them directly.
*) All enumerated types implicitly extend the java.lang.Enum class
*) Enumerated values are public static and final.
*) Enum can be declared with only the public or default modifier.
*) Enums can be declared outside a class or inside a class, however they must not be declared within a
method.
*) The items in an enumerated list are surprisingly called Enums.
enum CoffeeSize {BIG, huge, OVERWHELMING}; // can be in all caps or lowercase
CoffeeSize cs = CoffeeSize.BIG;
class Coffee {
CoffeeSize size;
}
class Coffee2 {
enum CoffeeSize {BIG, HUGE, OVERWHELMING}; // semi colon is optional
CoffeeSize size;
}
5) Interfaces :-
*) All interface methods are implicitly public and abstract.
*) All interface variables must be public, static and final.
*) A class can extend only one class and implement many interfaces.
*) A class cannot implement another class
*) Interfaces cannot implement another interface or class, but can extend another interface.
*) An interface is implicitly/default abstract.
6) Abstract Classes
*) Abstract methods cannot be synchronized.
Scenario #1 :-
You have the following class, can I use the abstract keyword at the class level.
package com.mywork.declaccess;
public class AbstractClass {
public abstract void m1(); // only abstract methods in class
}
Answer :- Yes we can use the abstract keyword at the class level
package com.mywork.declaccess;
public abstract class AbstractClass {
public abstract void m1(); // only abstract methods in class
}
Scenario #2 :-
You have both the abstract methods and concrete methods. Can I use the abstract keyword at the class level.
package com.mywork.declaccess;
public class AbstractClass {
public abstract void m1();
Answer :- Yes we can use the abstract keyword at the class level
package com.mywork.declaccess;
public abstract class AbstractClass {
public abstract void m1();
Scenario #3 :-
You have only oncrete methods. Can I use the abstract keyword at the class level.
package com.mywork.declaccess;
public class AbstractClass {
public void m2() {
System.out.println("This is a concrete method...");
}
}
Answer :- Yes we can use the abstract keyword at the class level
package com.mywork.declaccess;
public abstract class AbstractClass {
public void m2() {
System.out.println("This is a concrete method...");
}
}
Q) Can I use final and abstract at class level or method level as shown below :-
public final abstract class AbstractClass {
}
(Or)
Answer :- No, final and abstract cannot be used together. Because the abstract methods have to be
implemented by the subclass and making the class/method final would prevent the subclass from overriding
the method.
Q) Can I use private and abstract at class level or method level as shown below :-
public private abstract class AbstractClass {
}
(Or)
Answer :-
Either we have to make the class B as abstract or we have to implement the method m1() in class B.
1) Overriding
b) The return type must be the same as, or a subtype of, the return type declared in the original overridden
method in the super class (Returning subtype does not work in Java 1.4 or below)
c) The access level can’t be more restrictive than the overridden method, i.e. If the method in the parent is
protected you cannot make the overridden method private, but you can make it public.
d) The access level can be less restrictive than that of the overridden method
e) Instance methods can be overridden only if they are inherited by the subclass. A subclass within the
same package as the instance’s super class can override any super class method that is not marked private
or final. A subclass in a different package can override only those non-final methods marked public or
protected (since protected methods are inherited by the subclass)
f) The overriding method CAN throw any unchecked (runtime) exceptions, regardless of whether the
overridden method declares the exception.
g) The overriding method MUST NOT throw checked exceptions that are new or broader than those
declared by the overridden method.
For eg., a method that declares a FileNotFoundException cannot be overridden by a method that
declares a SQLException, Exception, or any other non-runtime exception unless it’s a subclass of
FileNotFoundException.
h) The overriding method can throw narrower or fewer exceptions. Just because an overridden method
takes risks doesn’t mean the overriding subclass exception takes the same risks.
Bottom line :-
An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what
the overridden method declares.
2) Overloading