snippetjournal

– notes, snippet codes, and my journal

Extends Inner Class, Example

leave a comment »

Dsini saya membuat suatu class public yang berisi 2 class lainnya, kemudian 2 class ini akan mengextends masing masing class di atas nya.

Ada cara yang berbeda ketika kita akan mau menextends class yang bukan public dan class ny sendiri adalah inner class.

berikut ini contoh nya :


package ASN.Coders;

/*
 * example how to extends inner class
 *
 */

public class Kampretos1 {

private String galau1;

public Kampretos1() {

}

private String methodkampretos(){
 galau1 = "class Kampretos1 <parent>";
 return galau1;
 }

protected class Kampretos2 extends Kampretos1{
 private String galau2;

private String methodsatu(){
 galau2 = "class Kampretos2 <child of parent>";
 return galau2;
 }
 }

protected class Kampretos3 extends Kampretos2{
 Kampretos3(){
 Kampretos1.this.super();
 }

 // membuat contstructor yang akan membuat instance class dari class kampretos1
 // yaitu mengambil reference dari parent pertama.
 // jika tidak begini inner class yang mengextends tidak akan bisa menemukan reference nya parent.


private String galau3;

private String methoddua(){
 galau3 = "class Kampretos3 <child of child of parent>";
 return galau3;
 }
 }

public static void main(String[] args) {
 String string;

Kampretos1 kampretos1 = new Kampretos1();
 string = kampretos1.methodkampretos();
 System.out.println("get variable value from method ==>> "+string);

Kampretos2 kampretos2 = kampretos1.new Kampretos2();
 string = kampretos2.methodsatu();
 System.out.println("get variable value from method ==>> "+string);

Kampretos3 kampretos3 = kampretos2.new Kampretos3();
 string = kampretos3.methoddua();
 System.out.println("get variable value from method ==>> "+string);

}
}

Written by snippetjournal

October 21, 2012 at 2:14 am

Posted in Programming

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: