• No results found

In Java not all classes have to be defined separate from each other You can put the definition of one class inside the definition of another class The inside class is called an inner class and the enclosing class is called an outer

byte stream

A 35: In Java not all classes have to be defined separate from each other You can put the definition of one class inside the definition of another class The inside class is called an inner class and the enclosing class is called an outer

class. So when you define an inner class, it is a member of the outer class in much the same way as other members like attributes, methods and constructors.

Q. Where should you use inner classes? Code without inner classes is more maintainable and readable. When you access private data members of the outer class, the JDK compiler creates package-access member functions in the outer class for the inner class to access the private members. This leaves a security hole. In general we should avoid using inner classes. Use inner class only when an inner class is only relevant in the context of the outer class and/or inner class can be made private so that only outer class can access it. Inner classes are used primarily to implement helper classes like Iterators, Comparators etc which are used in the context of an outer class. CO

Member inner class Anonymous inner class

public class MyStack { private Object[] items = null; …

public Iterator iterator() { return new StackIterator(); }

//inner class

class StackIterator implements Iterator{

public boolean hasNext(){…} }

}

public class MyStack { private Object[] items = null; …

public Iterator iterator() { return new Iterator {

public boolean hasNext() {…} }

} }

Explain outer and inner classes?

Class Type Description Example + Class name

Outer class

Package member class or interface

Top level class. Only type JVM can recognize.

//package scope class Outside{} Outside.class Inner

class static nested class or interface

Defined within the context of the top-level class. Must be static & can access static members of its containing class. No relationship between the instances of outside and Inside classes.

//package scope class Outside {

static class Inside{ } }

Outside.class ,Outside$Inside.class Inner

class

Member class Defined within the context of outer class, but non-static. Until an object of Outside class has been created you can’t create Inside. class Outside{ class Inside(){} } Outside.class , Outside$Inside.class Inner class

Local class Defined within a block of code. Can use final local variables and final method parameters. Only visible within the block of code that defines it.

class Outside { void first() { final int i = 5; class Inside{} } } Outside.class , Outside$1$Inside.class

Inner class

Anonymous class

Just like local class, but no name is used. Useful when only one instance is used in a method. Most commonly used in AWT/SWING event model, Spring framework hibernate call back methods etc.

//AWT example class Outside{ void first() {

button.addActionListener ( new ActionListener() {

public void actionPerformed(ActionEvent e) { System.out.println(“The button was pressed!”); }

}); } }

Outside.class , Outside$1.class

Note: If you have used the Spring framework with the Hibernate framework (Both are very popular frameworks, Refer section “Emerging Technologies/Frameworks”), it is likely that you would have used an anonymous inner class (i.e. a class declared inside a method) as shown below:

//anonymous inner classes can only access local variables if they are declared as final public Pet getPetById(final String id) {

return (Pet) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) {

HibernateTemplate ht = getHibernateTemplate(); // … can access variable “id”

return myPet; }

}); }

Q. Are the following valid java statements?

Line: OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Yes. The above line is valid. It is an instantiation of a static nested inner class.

OuterClass outerObject = new OuterClass();

Line: OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Yes. The above line is valid. It is an instantiation of a member inner class. An instance of an inner class can exist only within an instance of an outer class. The sample code for the above is shown below:

public class OuterClass {

static class StaticNestedClass { StaticNestedClass(){ System.out.println("StaticNestedClass"); } } class InnerClass { InnerClass(){ System.out.println("InnerClass"); } } }

Q 36:

What is type casting? Explain up casting vs. down casting? When do you get ClassCastException? LF DP FAQ

A 36:

Type casting means treating a variable of one type as though it is another type.

When up casting primitives as shown below from left to right, automatic conversion occurs. But if you go from right to left, down casting or explicit casting is required. Casting in Java is safer than in C or other languages that allow arbitrary casting. Java only lets casts occur when they make sense, such as a cast between a float and an int. However you can't cast between an int and a String (is an object in Java).

byte Æ short Æ int Æ long Æ float Æ double int i = 5;

long j = i; //Right. Up casting or implicit casting byte b1 = i; //Wrong. Compile time error “Type Mismatch”.

When it comes to object references you can always cast from a subclass to a superclass because a subclass object is also a superclass object. You can cast an object implicitly to a super class type (i.e. upcasting). If this were not the case polymorphism wouldn’t be possible.

Object

Vehicle

Car Bus

BMW

Vehicle v1 = new Car(); //Right.upcasting or implicit casting Vehicle v2 = new Vehicle();

Car c0 = v1; //Wrong. compile time error "Type Mismatch". //Explicit or down casting is required

Car c1 = (Car)v1; // Right. down casting or explicit casting. // v1 has knowledge of Car due to line1

Car c2 = (Car)v2; //Wrong. Runtime exception ClassCastException //v2 has no knowledge of Car.

Bus b1 = new BMW(); //Wrong. compile time error "Type Mismatch" Car c3 = new BMW(); //Right.upcasting or implicit casting

Car c4 = (BMW)v1; //Wrong. Runtime exception ClassCastException Object o = v1; //v1 can only be upcast to its parent or

Car c5 = (Car)v1; //v1 can be down cast to Car due to line 1. Upcasting vs Downcasting

You can cast down the hierarchy as well but you must explicitly write the cast and the object must be a

legitimate instance of the class you are casting to. The ClassCastException is thrown to indicate that code has attempted to cast an object to a subclass of which it is not an instance. If you are using J2SE 5.0 then “generics” will eliminate the need for casting (Refer Q55 in Java section) and otherwise you can deal with the problem of incorrect casting in two ways:

ƒ Use the exception handling mechanism to catch ClassCastException.

try{

Object o = new Integer(1);

System.out.println((String) o); }

catch(ClassCastException cce) {

logger.log(“Invalid casting, String is expected…Not an Integer”); System.out.println(((Integer) o).toString());

}

ƒ Use the instanceof statement to guard against incorrect casting.

if(v2 instanceof Car) { Car c2 = (Car) v2; }

Design pattern: The “instanceof” and “typecast” constructs are shown for the illustration purpose only. Using these constructs can be unmaintainable due to large if and elseif statements and can affect performance if used in frequently accessed methods or loops. Look at using visitor design pattern to avoid these constructs where applicable. (Refer Q11 in How would you go about section…).

Points-to-ponder: You can also get a ClassCastException when two different class loaders load the same class because they are treated as two different classes.

Outline

Related documents