can a local override final method? - java

public class Test {
public static void main(String args[]){
Test t = new Test();
t.inner();
}
public final void print() {
System.out.print("main");
}
public void inner() {
class TestInner {
void print(){
System.out.print("sub");
}
}
TestInner inner =new TestInner();
inner.print();
print();
}
}
Out put : submain
Question : the method print() in class Test is final and is accessible to local class , but still local class is a able to define print() method again how?
If we declare private final x() in super class, it is not accessible in sub class so we can define x() in sub class.
If we declare public final x() in super class, it is accessible in sub class so we can not define x() in sub class.
Can anybody explain ?

The inner class is not overriding the final method.
The inner class would have to extend the outer class for it to be able to override a method from the outer class.
The two classes are separate and unrelated to each other, other than the outer class contains the inner one.

Because the TestInner class doesn't extend the Test class, so it has its own namespace that is separate.
There is no trick to it, it isn't overwriting the Test classes print method.

The inner class is not overriding the final method.
The inner class would have to extend the outer class for it to be able to override a method from the outer class.
The two classes are separate and unrelated to each other, other than the outer class contains the inner one.

Related

how to access the outer class members in subclass wherin inner class is inherited

//below class is the example where in subclass extends the innerclass and from the subclass i am trying to access the methods of outer class i.e encapsulating class of inner class.
package innerClass;
public class outterclass {
private int outer=24;
protected int get_outer(){
return outer;
}
protected static class innerclass{
private int outer=25;
protected int get_outer(){
return outer;
}
}
}
package innerClass;
public class subclass_B extends outterclass.innerclass {
void parent_class_info_fetch(){
System.out.println(get_outer());
//i want to access the outer class get_outer method and how do i achieve that?
}
public static void main(String[] args) {
InheritanceStaticInnerClass_B isb=new InheritanceStaticInnerClass_B();
isb.parent_class_info_fetch();
}
}
Your innerclass is not an inner class. It is a static nested class and bears no special relationship to its enclosing class. You cannot reach an instance of the enclosing class because no such instance is available to innerclass or its subclasses.
If innerclass was indeed inner, then you would have to instantiate it with an enclosing instance:
outterclass outer = new outerclass();
subclass_B b = outer.new subclass_B();
Then, in parent_class_info_fetch() you could write
outterclass.this.get_outer()
to reach that method.
Of course, there would be several layers of bad practices in such code, so consider this just an academic execrise.
You should also learn about the basic naming conventions in Java.
The class outterclass.innerclass is a static class field, which means you don't necessarily have an enclosing instance of outterclass. On the other hand, the method get_outer of outterclass is an instance method, so you'll need the enclosing instance to call it.
With the class hierarchy you have, you'd have to make get_outer static (which requires making outer static as well).

How to acces to an Inner class, which is in an Enum in java

i want to know how i can access to a inner class, which is in an Enum.
Example:
public enum myEnum{
public class myInnerClass{
public void aMethod(){
//do somethink.....
}
}
}
How can i access to this class(access to it methods) in another class?
Thanks for your help :)
You can access the inner class inside the enum using it's instance, defined by enum fields:
public enum MyEnum{
INSTANCE_A,
INSTANCE_B;
public class MyInnerClass {
// This is no different from the inner class in a normal class
public String show() {
// You can get the name of the instance for which this method was called.
System.out.println(MyEnum.this.name());
return "Hello";
}
}
}
Now, to create an instance of MyInnerClass, you would do:
MyEnum instanceA = MyEnum.INSTANCE_A;
MyEnum.MyInnerClass myInnerInstance = instanceA.new MyInnerClass();
System.out.println(myInnerInstance.show());
Output:
INSTANCE_A // For MyEnum.this.name()
Hello
The way is similar to how you would do for an inner class, which is inside a normal class. There is no difference.
I mean calling methods of the inner class
To be able to call methods of inner class you'll need an instance of inner class (except for the case when inner class is static and you want to call a static method).
To create an object of innerClass you can do SSS.INSTANCE.new A(); (class from below)
Or you can declare it static then you'll work with it as with normal class.
public enum SSS {
INSTANCE;
public static class A {
}
public static void main(String[] args) {
SSS.A a = new SSS.A();
}
}
try this
Class cls = myEnum.myInnerClass.class;

How is it possible to create instance outside class of local inner class having private constructor?

Consider the following program:
public class A
{
public static void main(String[] args)
{
class B
{
private B()
{
System.out.println("local");
}
}
// how are we able to create the object of the class having private constructor
// of this class.
B b1= new B();
System.out.println("main");
}
}
Output:
local
main
A class having private constructor means we can create object inside the class only, but here am able to create instance outside the class. can someone explain how are we able to create the object of B outside class B??
Because a Top Level Class is a happy family, everyone can access one another despite private.
JLS 6.6.1
6.6.1. Determining Accessibility
A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
You're allowed to even access private variables of that class too (try it!).
This is because you are defining that class inside the same class your calling it from, so you have private/internal knowledge of that class.
If you move Class B outside of Class A, it will work as expected.
Refer the JLS 6.6.1:
Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
The way this is implemented is using synthetic package-protected methods.
"If you like to hide the private members of your inner class, you may define an Interface with the public members and create an anonymous inner class that implements this interface. Refer to this code:"
class ABC{
private interface MyInterface{
public void printInt();
}
private static MyInterface mMember = new MyInterface(){
private int x=10;
public void printInt(){
System.out.println(String.valueOf(x));
}
};
public static void main(String... args){
System.out.println("Hello :: "+mMember.x); ///not allowed
mMember.printInt(); // allowed
}
}
You said that
A class having private constructor means we can create object inside the class only
But
Thats happened because you define your inner Class in main(String[] args) method Not in Top Level Class
If you try that
public class A {
class B {
private B() {
System.out.println("local");
}
}
public static void main(String[] args) {
B b1 = new B(); // you cant create object of inner Class B
System.out.println("main");
}
}
Then you cant create object of inner Class B

Inheriting InnerClass in java

public class InnerClass {
class Inner
{
public void method()
{
System.out.println("Innerclass");
}
}
}
class Sample extends InnerClass.Inner
{
public static void main(String [] arg)
{
Sample s = new Sample(new InnerClass());
s.method();
}
//why is this mandatory???
Sample(InnerClass i) {
i.super();
}
#Override
public void method() {
System.out.println("derived class");
}
}
when i make a class that derives from an innerclass (Innerclass.Inner) default constructor doesn't works. later i came to know that it requires to include a constructor taking Enclosing class reference why is it so?
Non static inner classes in Java have an implicit reference to the enclosing instance. You can solve your problem with:
public class InnerClass {
static class Inner // can make it public too
{
public void method()
{
System.out.println("Innerclass");
}
}
}
Just don't expect to be able to call any methods on InnerClass without a specific instance.
Because non-static inner classes have an implicit member that points back to their outer class, and you can't create an instance of the inner class without giving it that pointer. If you directly create an instance of an inner class, you have to use new outer.Inner() (or it might be outer.new Inner(), I can never remember). But Sample isn't an inner class, it just inherits one, so the outer instance must be passed in its constructor to the base constructor. Thus, it needs to have some instance of outer available, or create it itself.

Must the inner class be static in Java?

I created a non-static inner class like this:
class Sample {
public void sam() {
System.out.println("hi");
}
}
I called it in main method like this:
Sample obj = new Sample();
obj.sam();
It gave a compilation error: non-static cannot be referenced from a static context When I declared the non-static inner class as static, it works. Why is that so?
For a non-static inner class, the compiler automatically adds a hidden reference to the "owner" object instance. When you try to create it from a static method (say, the main method), there is no owning instance. It is like trying to call an instance method from a static method - the compiler won't allow it, because you don't actually have an instance to call.
So the inner class must either itself be static (in which case no owning instance is required), or you only create the inner class instance from within a non-static context.
A non-static inner class has the outer class as an instance variable, which means it can only be instantiated from such an instance of the outer class:
public class Outer{
public class Inner{
}
public void doValidStuff(){
Inner inner = new Inner();
// no problem, I created it from the context of *this*
}
public static void doInvalidStuff(){
Inner inner = new Inner();
// this will fail, as there is no *this* in a static context
}
}
An inner class needs an instance of the outer class, because there is an implicit constructor generated by compiler. However you can get around it like the following:
public class A {
public static void main(String[] args) {
new A(). new B().a();
}
class B {
public void a() {
System.err.println("AAA");
}
}
}
Maybe this will help : http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
the non-static inner class cannot be called in a static context (in your example there is no instance of the outer class).

Categories

Resources