Topics

6/recent/ticker-posts

Different types of Java Part 3

 Different types of Java Part 3


final class

  • Classes which do not want to be derived into subclasses are declared as final. For final classes no child classes hierarchy exist. All wrapper classes such as Integer, Long, Float, Double, String, String Buffer are all final classes. 
  • Math class is also final in Java API. Classes whose functions do not want to be overridden in sub classes are declared as final. For instance if a class contains 5 final and 5 non final methods. 
  • Then such a class is declared as normal class. If all the 10 methods of a class want to be declared as final, instead of declaring all the functions as final, declare class itself as final. Because all the methods are final they are static too. Hence we can call the methods using classname.methodname().
  •  But it not mandatory/no guarantee that final class final methods may be static.

inner class/nested class (class inside a class)

  • Class written in a class is called as inner class. to class only default and public access specifiers can be applied but to inner classes protected and private access specifier can also be applied because like variable and method inner class is also a member of outer class.
  •  If inner class and outer class are declared as public and kept in a package they can be accessible anywhere outside the package. 
  • If outer is declared as public and inner class is declared as default then inner class can be accessible only within the package. If outer class is public and inner class is protected then inner class can be accessible only up to sub class outside the package. 
  • If inner class is private the class can be accessible only up to within the methods of outer class.


If inner class is non-static, then to access inner class, outerclass object must be created first, then with the outer class object reference.new innerClass() can be called. 


  • non-static inner class can contain only non-static methods. 
  • static inner class can contain both static and non-static methods.


static inner class static method can be called using OuterClass.InnerClass.method()

static inner class non-static method can be accessed using static inner class object.method(). static inner class object can be created using OuterClass.InnerClass ocic=new OuterClass.InnerClass();


inner class can access outer class non-static variables and methods. But outer class cannot access inner class variables and methods.

inner class non-static methods can access non-static variable and methods of outer class.

But static-inner class, static methods can be access only static variables and methods  of outer class.

this and super keyword can be used only in non-static inner class, but not in static-inner class.

access modifiers that can be applied on inner class are:

static, final, abstract, private, default, protected and public


//OC.java (OC means OuterClass)

class OC {

int i; // non-static variable/instance variable

static int j; // static variable/class variable

void print1() {

System.out.println(i); // Yes

System.out.println(j); // Yes

}

static void print2() {

// System.out.println(i); // No

System.out.println(j); // Yes

}

// non-static inner class

class NSIC {

void print3() {

System.out.println(i); // Yes

System.out.println(j); // Yes

}

/* static void print4() {

// System.out.println(i); // Yes

System.out.println(j); // Yes

}*/

}

// static inner class

static class SIC {

void print5() {

// System.out.println(i); // Yes

System.out.println(j); // Yes

}

static void print6() {

// System.out.println(i); // Yes

System.out.println(j); // Yes

}

}

} // end of OC


Points about how to write inner class:


i) In non-static inner class we can write only non-static method, but cannot write static method. 

ii) In that we can access both static and non-static variables of outer class.


ii) In static inner class we can write both non-static and static method. 

iv) In those methods we can access only static variables of outer class.


// OCICClient.java

class OCICClient {


public static void main(String rags[]) {

// how to access non-static method of outer class?

OC oc=new OC();

oc.print1();

// how to access static method of outer class?

OC.print2();

// how to access non-static method of non-static inner class?

// first of all know how to instantiate non-static inner class?

OC.NSIC ocnsic=oc.new NSIC();

ocnsic.print3();

// how to access non-static method of static inner class?

// how to create object of static inner class

OC.SIC ocsic=new OC.SIC();

ocsic.print5();

// how to access static method of static inner class

OC.SIC.print6();

}

}


Points about how to access inner class:

A class can contain both non-static and static inner classes.


Non-static inner class can contain only non-static method

Static inner class can contain both static and non-static methods


In non-static inner class non-static method we can access both static and non-static variables of outer class

In static inner class both static and non-static methods we can access only static variable of outer class


How to create object of non-static inner class?

OC oc=new OC();

OC.NSIC ocnsic=oc.new NSIC();

ocnsic.print3();


How to create object of static inner class?

OC.SIC ocsic=new OC.SIC();

ocsic.print5();

OC.SIC.print6();

Post a Comment

0 Comments