Extending an abstract inner class in java
In the earlier posts, I mentioned about inner classes. For the reference we can see in the below link.
For extending an abstract inner class we have few approaches:-
We can extend abstract inner class in following ways
Example to extend abstract inner class
AbstractNonStaticInnerClass.java
package com.gaurav.corejava.innerclass;
public class AbstractNonStaticInnerClass {
private int secondNumber = 10;
public int getCalculatedValue() {
SubNonStaticInnerClass nsic = new SubNonStaticInnerClass();
int firstNumber = nsic.getFirstNumber();
return firstNumber + secondNumber;
}
abstract class NonStaticInnerClass {
int firstNum = 20;
public abstract String getName();
public String strNonStInVar = "MYSELF NON STATIC INNER CLASS VARIABLE ";
public int getFirstNumber() {
return firstNum;
}
}
class SubNonStaticInnerClass extends NonStaticInnerClass {
String name = "ARYAN GAURAV";
public String getName() {
return name;
}
}
public static void main(String args[]) {
// FIRST WAY TO CREATE AN INSTANCE OF NON STATIC INNER CLASS
AbstractNonStaticInnerClass nsictd = new AbstractNonStaticInnerClass();
AbstractNonStaticInnerClass.NonStaticInnerClass nonStaticInner = nsictd.new SubNonStaticInnerClass();
System.out.println("******************************************");
System.out
.println("GETTING FIRST NUMBER FROM NON STATIC INNER CLASS :- "
+ nonStaticInner.getFirstNumber());
System.out.println("GETTING CALCULATED VALUE :- "
+ nsictd.getCalculatedValue());
// SECOND WAY TO CREATE AN INSTANCE OF NON STATIC INNER CLASS
System.out.println("\n******************************************");
AbstractNonStaticInnerClass.NonStaticInnerClass nonstInner = (new AbstractNonStaticInnerClass()).new SubNonStaticInnerClass();
System.out
.println("GETTING FIRST NUMBER FROM NON STATIC INNER CLASS WITH SECOND APPROACH :- "
+ nonstInner.getFirstNumber());
System.out
.println("GETTING STRING VALUE FROM NON STATIC INNER CLASS WITH SECOND APPROACH :- "
+ nonstInner.strNonStInVar);
System.out
.println("CALLING METHOD OF SUB CLASS NON STATIC INNER CLASS-"
+ nonstInner.getName());
}
}
public class AbstractNonStaticInnerClass {
private int secondNumber = 10;
public int getCalculatedValue() {
SubNonStaticInnerClass nsic = new SubNonStaticInnerClass();
int firstNumber = nsic.getFirstNumber();
return firstNumber + secondNumber;
}
abstract class NonStaticInnerClass {
int firstNum = 20;
public abstract String getName();
public String strNonStInVar = "MYSELF NON STATIC INNER CLASS VARIABLE ";
public int getFirstNumber() {
return firstNum;
}
}
class SubNonStaticInnerClass extends NonStaticInnerClass {
String name = "ARYAN GAURAV";
public String getName() {
return name;
}
}
public static void main(String args[]) {
// FIRST WAY TO CREATE AN INSTANCE OF NON STATIC INNER CLASS
AbstractNonStaticInnerClass nsictd = new AbstractNonStaticInnerClass();
AbstractNonStaticInnerClass.NonStaticInnerClass nonStaticInner = nsictd.new SubNonStaticInnerClass();
System.out.println("******************************************");
System.out
.println("GETTING FIRST NUMBER FROM NON STATIC INNER CLASS :- "
+ nonStaticInner.getFirstNumber());
System.out.println("GETTING CALCULATED VALUE :- "
+ nsictd.getCalculatedValue());
// SECOND WAY TO CREATE AN INSTANCE OF NON STATIC INNER CLASS
System.out.println("\n******************************************");
AbstractNonStaticInnerClass.NonStaticInnerClass nonstInner = (new AbstractNonStaticInnerClass()).new SubNonStaticInnerClass();
System.out
.println("GETTING FIRST NUMBER FROM NON STATIC INNER CLASS WITH SECOND APPROACH :- "
+ nonstInner.getFirstNumber());
System.out
.println("GETTING STRING VALUE FROM NON STATIC INNER CLASS WITH SECOND APPROACH :- "
+ nonstInner.strNonStInVar);
System.out
.println("CALLING METHOD OF SUB CLASS NON STATIC INNER CLASS-"
+ nonstInner.getName());
}
}
Result:-
******************************************
GETTING FIRST NUMBER FROM NON STATIC INNER CLASS :- 20
GETTING CALCULATED VALUE :- 30
******************************************
GETTING FIRST NUMBER FROM NON STATIC INNER CLASS WITH SECOND APPROACH :- 20
GETTING STRING VALUE FROM NON STATIC INNER CLASS WITH SECOND APPROACH :- MYSELF NON STATIC INNER CLASS VARIABLE
CALLING METHOD OF SUB CLASS NON STATIC INNER CLASS-ARYAN GAURAV
GETTING FIRST NUMBER FROM NON STATIC INNER CLASS :- 20
GETTING CALCULATED VALUE :- 30
******************************************
GETTING FIRST NUMBER FROM NON STATIC INNER CLASS WITH SECOND APPROACH :- 20
GETTING STRING VALUE FROM NON STATIC INNER CLASS WITH SECOND APPROACH :- MYSELF NON STATIC INNER CLASS VARIABLE
CALLING METHOD OF SUB CLASS NON STATIC INNER CLASS-ARYAN GAURAV
Example to extend abstract inner class using second way i.e Anonymous class
CallAbstractNonStaticInnerClass.java
package com.gaurav.corejava.innerclass;
public class CallAbstractNonStaticInnerClass {
private int secondNumber = 10;
abstract class NonStaticInnerClass {
int firstNum = 20;
public abstract String getName();
public String strNonStInVar = "MYSELF ABSTRACT NON STATIC INNER CLASS VARIABLE ";
public int getCalculatedNumber() {
return firstNum + secondNumber;
}
}
}
public class CallAbstractNonStaticInnerClass {
private int secondNumber = 10;
abstract class NonStaticInnerClass {
int firstNum = 20;
public abstract String getName();
public String strNonStInVar = "MYSELF ABSTRACT NON STATIC INNER CLASS VARIABLE ";
public int getCalculatedNumber() {
return firstNum + secondNumber;
}
}
}
ExtendAbstractNonStaticInnerClass.java
package com.gaurav.corejava.innerclass;
public class ExtendAbstractNonStaticInnerClass {
public static void main(String args[]){
CallAbstractNonStaticInnerClass.NonStaticInnerClass nest = (new CallAbstractNonStaticInnerClass()).new NonStaticInnerClass(){
@Override
public String getName() {
System.out.println("INSIDE GETNAME() METHOD WHICH IS ABSTRACT METHOD IN NON STATIC INNER CLASS");
return "GAURAV";
}};
System.out.println("THE STRING VALUE RETURNED BY GETNAME METHOD IS : - "+nest.getName());
System.out.println("ACCESSING INT VARIABLE OF THE ABSTRACT INNER CLASS :- "+nest.firstNum);
System.out.println("ACCESSING STRING VARIABLE OF THE ABSTRACT INNER CLASS :- "+nest.strNonStInVar);
System.out.println("ACCESSING GETCALCULATEDNUMBER METHOD OF THE ABSTRACT INNER CLASS :-"+nest.getCalculatedNumber());
}
}
public class ExtendAbstractNonStaticInnerClass {
public static void main(String args[]){
CallAbstractNonStaticInnerClass.NonStaticInnerClass nest = (new CallAbstractNonStaticInnerClass()).new NonStaticInnerClass(){
@Override
public String getName() {
System.out.println("INSIDE GETNAME() METHOD WHICH IS ABSTRACT METHOD IN NON STATIC INNER CLASS");
return "GAURAV";
}};
System.out.println("THE STRING VALUE RETURNED BY GETNAME METHOD IS : - "+nest.getName());
System.out.println("ACCESSING INT VARIABLE OF THE ABSTRACT INNER CLASS :- "+nest.firstNum);
System.out.println("ACCESSING STRING VARIABLE OF THE ABSTRACT INNER CLASS :- "+nest.strNonStInVar);
System.out.println("ACCESSING GETCALCULATEDNUMBER METHOD OF THE ABSTRACT INNER CLASS :-"+nest.getCalculatedNumber());
}
}
Result:-
INSIDE GETNAME() METHOD WHICH IS ABSTRACT METHOD IN NON STATIC INNER CLASS
THE STRING VALUE RETURNED BY GETNAME METHOD IS : - GAURAV
ACCESSING INT VARIABLE OF THE ABSTRACT INNER CLASS :- 20
ACCESSING STRING VARIABLE OF THE ABSTRACT INNER CLASS :- MYSELF ABSTRACT NON STATIC INNER CLASS VARIABLE
ACCESSING GETCALCULATEDNUMBER METHOD OF THE ABSTRACT INNER CLASS :-30
THE STRING VALUE RETURNED BY GETNAME METHOD IS : - GAURAV
ACCESSING INT VARIABLE OF THE ABSTRACT INNER CLASS :- 20
ACCESSING STRING VARIABLE OF THE ABSTRACT INNER CLASS :- MYSELF ABSTRACT NON STATIC INNER CLASS VARIABLE
ACCESSING GETCALCULATEDNUMBER METHOD OF THE ABSTRACT INNER CLASS :-30
No comments:
Post a Comment