You are on page 1of 7

Java 1.

5 Notes
1) Please say something about yourself?

2) Explain OOPs concepts.


a) Abstraction :-
b) Encapsulation :-
c) Inheritance
d) Polymorphism

3) What is the difference between and abstract class and an interface?

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

Do you know hibernate? If yes,


Then can I have a pojo class with a parameterized constructor?
No I cannot have a parameterized constructor in my POJO class because when there is a parameterized
constructor the jvm does not add the default constructor and when Hibernate uses the pojo it tries to create
an object using reflection and then it would search for the default constructor and since the jvm did not add
it the construction of the object will fail.

Chapter 1 :- Declarations and Access Control


1) null, true, false are not keywords
Access modifiers cannot be applied to local variables, only final can be used with local variables.
Synchronized methods can be overridden to be non-synchronized and vice-versa.
A synchronized method can make calls to non-synchronized methods.
An array is an object and all array types implement the Serializable and Clonable interfaces.

Datatypes

0/1 -2n-1., . . . ., 0, 1, 2, . . . . . . . . . . . . . . . . . . . 2n-1 - 1


Sign Bit Data bits (n-1 bits would be the data bits)

The size of the data including the sign would be = n bits

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.

The negative range would start at (-2 power (n-1)) to -1


The positive range would start at 0, 1 till ((-2 power (n-1)) -1(this is for the zero))
Type No. of Bits Bytes Min Range Max Range Comments
byte 8 1 -27 27 - 1 1 sign bit, (2 power 7)
value bits
short 16 2 -215 215 - 1 1 sign bit, (2 power 15)
value bits
int 32 4 -231 231 - 1 1 sign bit, (2 power 31)
value bits
long 64 8 -263 263 – 1 1 sign bit, (2 power 63)
value bits
float 32 4 NA NA
double 64 8 NA NA
boolean VM Dependent - false True
char 2 power 16 0 216 - 1

2) Variable args :- void doStuff (int… x)


*) the parameter must have the datatype followed by three dots followed by space followed by the variable
*) the datatype can be primitive or object
*) It is legal to have other parameters in a method that uses var-args
*) The var-args must be the last parameter in the method signature, and you can have only one var-args in a
method.

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 :-

I have the following method signature using var-args


void doStuff (int x, String… str);
The above var-args methods can be called as follows :-

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;

*) Declaring an enum outside a class


enum CoffeeSize {BIG, HUGE, OVERWHELMING};

class Coffee {
CoffeeSize size;
}

public class CoffeeTest {


public static void main (String.. args) {
Coffee drink = new Coffee();
drink.size = CoffeeSize.BIG;
}
}

*) Declaring an enum inside a class

class Coffee2 {
enum CoffeeSize {BIG, HUGE, OVERWHELMING}; // semi colon is optional
CoffeeSize size;
}

public class CoffeeTest2 {


public static void main (String.. args) {
Coffee drink = new Coffee();
drink.size = Coffee2.CoffeeSize.BIG;
}
}

*) Declaring an enum inside a method is not legal


public class CoffeeTest1 {
public static void main (String.. args) {
// Wrong !! cannot declare enums in methods
enum CoffeeSize {BIG, HUGE, OVERWHELMING}; // semi colon is optional

Coffee drink = new Coffee();


drink.size = CoffeeSize.BIG;
}
}

*) Declaring an enum that has constructors


TODO

4) What is the difference between Abstract Classes and Interfaces?

Abstract Class Interface


Used when you want to group a certain functionality Used when there is no common functionality to be
or part of a functionality and make it available to all shared between the child classes.
the child classes.
Can have a constructor where you can initialize the Cannot have any constructors or instance variables.
instance variables (check this)
Can have static final variables Can have static final variables
Belongs to one hierarchy It can be part of multiple hierarchies
The class can have both abstract methods and Can have only abstract methods
concrete methods
Can make the class abstract even when there are no
abstract methods in it

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();

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 abstract void m1();

public void m2() {


System.out.println("This is a concrete method...");
}
}

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 instantiate an abstract class?


No, we cannot instantiate an abstract class.

Q) Can I have a constructor in an abstract class? The correct answer is yes.


If the answer is yes then ask why because anyway we cannot instantiate the class, so what is the use of
having a constructor in an abstract class.
The constructor can be used to initialize instance variables (that would be used in the subclasses as well).

Q) Can I use final and abstract at class level or method level as shown below :-
public final abstract class AbstractClass {
}

(Or)

public abstract class AbstractClass {


public final abstract void m1();
}

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)

public abstract class AbstractClass {


private final abstract void m1();
}

Answer :- No, private and abstract cannot be used together.


Because the abstract methods have to be implemented by the subclass and making the method private
would make the method invisible to the subclass and so cannot override the method in the subclass.
We cannot use private at the class level, only public or protected can be used at the class level.

Scenario :- I have the following :-


public abstract class A {
private final abstract void m1();
}

public class B extends A {


private final abstract void m1();
}

Can I leave the abstract method unimplemented in the subclass B.

Answer :-
Either we have to make the class B as abstract or we have to implement the method m1() in class B.

Chapter 2 :- Object Orientation

1) Overriding

*) Rules for Overriding a method are as follows :-


a) The argument list must exactly match that of the overridden method. If they don’t match, you end up
with an overloaded method you didn’t intend.

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.

i) You cannot override a method marked final.

j) You cannot override a method marked static.

k) If a method can’t be inherited, you cannot override it.

Examples of legal and illegal method overrides


public class Animal {
public void eat() {}
}

Illegal Override Code Problem with the code


private void eat() Access modifier is more restrictive
pblic void eat() throws IOException Declares a checked exception not defined by the super class version
public void eat(String food) {} A legal overload, not an override because the argument list changed
Public String eat() {} Not an override because of the return type
Not an overload because there is no change in the argument list

2) Overloading

*) Rules for overloading methods


a) Overloaded methods MUST change the argument list.

b) Overloaded methods CAN change the return type.

c) Overloaded methods CAN change the access modifier.

d) Overloaded methods CAN declare new or broader checked exceptions

e) A method can be overloaded in the same class or its subclass.

You might also like