Saturday, December 11, 2010

Inner classes | Local Inner Classes | Anonymous Inner Classes | When to Use an Anonymous Class |When to Use an Inner Class



Inner Classes

  • Inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields.
  • It’s allowed only final variable.
  • Also, because an inner class is associated with an instance, it cannot define any static members itself.
  • Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:
class OuterClass {
...
class InnerClass {
...
}
}
  • To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass()


Local Inner Classes

  • We can declare an inner class within the body of a method. Such a class is known as a local inner class.
  • A local class is a nested class that is not a member of any class and that has a name.
  • Every local class declaration statement is contained by a block.
  • Local class defined in a method has access to final method variables and also to the outer class's member variables.

Anonymous Inner Classes

  • We can declare an inner class within the body of a method without naming its called anonymous inner classes.
  • An anonymous class is an inner class with the usual
  • class body, but
  • No class name
  • No access specifier (i.e., no public/private/protected)
  • No constructor
  • No explicit use of extends or implements
  • It either extends one class or implements one interface
  • An anonymous class is never abstract. An anonymous class is always an inner class; it is never static. An anonymous class is always implicitly final.
  • Are commonly used in Swing code to implement listeners or adapters.

Syntax for Anonymous Classes

new class-name ( [ argument-list ] ) { class-body }
or:
new interface-name () { class-body }

When to Use an Anonymous Class
  • The class has a very short body.
  • Only one instance of the class is needed.
  • The class is used right after it is defined.
  • The name of the class does not make your code any easier to understand.

Static variable and method


Static Keyword

  • The static variables are created only once for all objects.
  • The value of static variables is the last modified value.
  • The static method only access the static variable.
  • The static keyword u can’t apply for constructor.
  • The static block is first executed. The static variable and method u can access from non-static method and constructor.
  • Without creating object we can access the Static members and methods.
  • It won’t support the this and super keyword inside the static method.
  • We can apply static keyword only inner class

Example of Static
public class Sta {
static int a=100; int b=200;
static void show(){
System.out.println(“non_static var=“+b); è // we can’t access non static variable
System.out.println("static show method"); inside of static method….
}
Static{ è // static block
System.out.println("static block");
}
static int disp(int b){
return b;
}
public static void main(String[] args){
System.out.println("static variable=="+Sta.a);
Sta.show();
System.out.println(Sta.disp(300));
}
}



Difference between Overloading and overriding

Overloading

  • Same method name with different argument.
  • Method return type is different and Access specifier is might be different or might be same.
  • Overloaded methods must have different argument lists (different of args, or different types).
  • It is resolved at the compile time.





Overriding
  • A subclass can redefine an existing method from a superclass
  • It is resolved at the runtime.
  • When overriding a method the subclass method prototype must match exactly the prototype of the superclass (same name, same return type, same arguments).
  • You may change access specifier (public, private, protected), but derived classes cannot decrease the visibility.
  • Instance method can override only instance method.
  • Static method can override only static method.
Diff b/w OverLoading & Overriding


Object Oriented Programming|opps| Encapsulation | Polymorphism | Inheritance

Object Oriented Programming

OOP encapsulates data (e.g. numbers and objects) and methods (functions)

Information hiding is a key aspect of OOP

Implementation details are hidden from the user
(“we can drive a car without knowing how it is built”)

Java is “object oriented” language.

Encapsulation
Data and methods (ie functions) are stored together in a “class”. Code becomes modifiable, understandable, and portable.

In OOP the data and methods associated with objects are normally hidden from the users.

The details of the code are not available outside the class, the author decides what others can see, modify, and use.

Variables (data) can be Public, Protected, Private, Default.

Inheritance
Objects can inherit the properties of other objects.
Inheritance permits software reusability.
New classes can be created from old classes.
The attributes (data) and behavior (methods) of the old class are available to the new class.
New data and methods can be added to the new class also.
Inheritance allows you to write key pieces of code once and use it over and over.
Java support Multilevel Inheritance.
Java does not support Multiple Inheritance.

Polymorphism
You can write functions that behave differently depending on what kind of data is passed to them. E.g. you could make a sqrt function that would work with integers, floats, or even complex numbers.
Polymorphism allows you to avoid very complicated switch or if/else constructs that are required in other languages to treat special cases.
The same method name can be used in many different ways by different objects, this is especially interesting for inherited objects.

Abstraction

An abstraction denotes the essentials properties and behavior of an Object that differentiate it from other objects.