Apr 30

What is the Output of the following Code?

public class department {

public static void main(String args [ ]){

group g = new group (){

public String Name() {

return(“abc”);

}

};
group.member gm = new group().new member(){

public String Name() {

return(“def”);

}

};

}

}
class
group {

public String Name (){

return (“ghi”);

}

abstract class member {

public abstract String Name();

}

}


Antwort/ answer
Tagged with:
Apr 29

While coming to the end of explanation of inner classes I have something more simple: static nested classes:

public class department{

static class Responsibility { // definition of static nested class

void getInfo (){

System.out.println(“Info Zuständigkeiten”);

}

}

public static void main(String args [ ]){

group.ActivityField ga = new group.ActivityField (); // implementation of static nested class – group
Responsibility r = new Responsibility(); // implementation of static nested class – department

ga.getInfo(); // no difference for calling method
r.getInfo(); // no difference for calling method

}

}
class group {

static class ActivityField{ // this is a definition of a static nested class as well

void getInfo (){

System.out.println(“Info ActivityField”);

}

}

}

So as promised, it is relatively easy; as long as classes are declared as static and have appropriate access-rights they can be embedded as every other class. It need only be kept in mind that static classes do not have access to instance variables and non static method of the outer class.

Tagged with:
Apr 28

Today we increase the difficulty once again :)

Until now we have only implemented relatively simple inner classes on the base of classes/ interfaces, but today we will involve additionally arguments.

For our example we have the following preconditions:

class group {

void setExample(Example a) { }

}

interface Example {

void ExampleInfo();

}

Here we have class group which is needing interface Example (respectively an instance of the class which is implementing this interface) for executing method setExample.

Now the tricky question how to implement an anonymous class of group which is needing as well an (anonymous) instance of Example?

Here the answer:

public class department {

public static void main(String args [ ]){

group g = new group (); // Attention: Here no definition of an anonymous inner class!
g.setExample (new Example(){ // here the needed argument is defined as anonymous inner class within the argument-transfer

public void ExampleInfo() { // implementation of the needed method of interface Example

System.out.println(“ExampleInfo”);

}

}); // Attention: we are derivating from the usual notation }; and closing instead (since we are working in argument-transfer) with });

}

}

Tagged with:
Apr 27

Please keep in mind that either an interface or a class can be the master for an anonymous class. First of all – as usual – an example to make it easier to understand:

public class department{

public static void main(String args [ ]){

group g = new group () {};

}

}
class group{}

interface group2 {}

While definition of class Gruppe { } other classes can be extended or interfaces implemented, e.g.

  • class group implements group2{}
  • class group implements group2, group3{}
  • class group extends department {}

Interfaces can be extended according to the usual rules, e.g.

  • interface group3 extends group2 {}

So far nothing new. Calling, compilation  and running of inner anonymous classes are not changed with the previous changes/ examples, but the anonymous class itself can not extend, respectively implement classes, respectively interfaces.

Here a few examples which lead to CompilerErrors:

  • group g = new group () implements group2 {};
  • group g = new group implements group2, group3 () {};
  • group g = new group extends department() {};
Tagged with:
Apr 26

Today an example of an anonymous inner class with interfaces instead an upper class:

public class department {

public static void main(String args [ ]){

group  g = new group() {

public void getInfo(){

System.out.println(“anonymous group”);

}

};

g.getInfo();

}

}

interface group {

public void getInfo();

}

As you can see, there are not much differences in comparison to the use of upper classes. It might confuse you that it looks like the impossible instantiation of an interface. In fact, an anonymous object is created which has interface group implemented.

Tagged with:
Apr 25

Inner classes are not allowed to have own methods which are not implemented in the upper class:

public class department {

public static void main(String args [ ]){

group g = new group () {

public void getInfo(){

System.out.println(“anonymous group”);

}
public void doSomething(){
// not legal, since method is not defined in upper class, but no CompilerError on this point

System.out.println(“Here I am);

}

};

g.getInfo(); //Output: anonymous group
g.doSomething();// CompilerError while implementing the use of the method

}

}
class group {

public void getInfo(){

System.out.println(“non anonymous group”);

}

}

Tagged with:
Apr 24

In case classes are at the same point (outside a method) declarated and instantiated we deal with inner anonymous classes. Therefore an object is created automatically which is only used for stringend local purposes. Since no class-name is given we speak about anonymous classes.

In order to use those anonymous inner classes either a simple interface or a super-class is needed which is providing the main port for the implementation.

Now enough of the boring theory, let’s have a look on an example which makes it hopefully easier:

public class department {

public static void main(String args [ ]){

group g = new group () { // Some kind of subclass of group is declarated and instantiated which does not have an own name

public void getInfo(){ // overwriting of method getInfo()

System.out.println(“anonyme Gruppe”);

}

}; // end of anonymous inner class which needs mandatory closed with ‘;’

}

}

class group { // definition of the original class – no inner class in this example!!!

public void getInfo(){ //original method of class group

System.out.println(“nicht-anonyme Gruppe”);

}

}

Tagged with:
Apr 23

To confuse you even more; please be aware that within static methods local classes can only access static outer variables:

public class department {

final String name3 =”Test3″;
static String name4 = “Test4″;
String name5 = “Test5″;

static void doSomething(){

String name1 =”Test1″;
final String name2 =”Test2″;
class newClass{

void getName() {

System.out.println(name1); // CompilerError since inside method and not final
System.out.println(name2);
// ok, since final
System.out.println(name3); // CompilerError, since eventually not initialized
System.out.println(name4);
// ok, static methods are able to access static variables since other variables might not be inialized ad this point of time
System.out.println(name5); // CompilerError

}

}
newClass a = new newClass();
a.getName();

}
public static void main(String args [ ]){

department a = new department();
a.doSomething();

}

}

Tagged with:
Apr 22

As already explained are inner classes able to access all variables/ objects of the outer class, but it is getting a littly bit tricky  in case a class is defined within a method of the outer class – the so called local inner classes which can only access final variables/ objects of the local class, but all objects of the outer class:

public class department {

String name =”Finance”;

void doSomething(){

String name1 =”Test1″;
final String name2 =”Test2″;
class newClass{

void getName() {

System.out.println(name1); //CompilerError since not final
System.out.println(name2); // Output: Test2

System.out.println(name); // access to variables/ objects of the outer class as usual; Output: Finance

}

}
newClass a = new newClass();
// creating an instance of newClass
a.getName(); // calling method getName of newClass

}
public static void main(String args [ ]){

department a = new department();
a.doSomething();

}

}

Tagged with:
Apr 21

Inner classes can also be instantiated within methods of the outer class:

public class department {

void doSomething(){

class newClass{ } // only definition, no instantiation
newClass a = new newClass();
// instantiation

}
public static void main(String args [ ]){

department a = new department();
a.doSomething();

}

}

class newClass is not known outside method doSomething and can therefore only used within this method.

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs