Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.89/5 (2 votes)
See more:
Ref. Class's Function Is Not accessible Why?

hear is my code

C#
public class FieldShadowing_1
{
    public  String name = "FieldShadowing_1";
    public   String getName()
    {
        return "Hello";
    }


}


C#
public class FieldShadowing_2 extends FieldShadowing_1
{



    public  void display()
    {
        System.out.println("Check");
    }
 }

C#
public static void main(String[] args)
   {

FieldShadowing_1 b = new FieldShadowing_2();

       
        System.out.println(b.display()); //display() is not accesible hear O"
}
Posted

Please see my comment to Solution 1 and Solution 2. They do not completely explain what's going on.

To understand it, you need to get the concept of compile-time and runtime types.

In FieldShadowing_1 b = new FieldShadowing_2(); compile-time type of b is FieldShadowing_1, but runtime type is FieldShadowing_2. You can think of it this way: b provides a reference to the instance of FieldShadowing_2 from the point of view of FieldShadowing_1. As those are references to instance type, you could have to references of different types which are physically pointing to the same place in memory. So, if you down-case b to FieldShadowing_2, it will give your the access to display. This is not a good idea, because in more general context, your compile-time information does not care information that this is the derived type. You would rather need to use dynamic cast which actually asks an instance which type is it, actually.

If those were the interface reference, the situation would be more complex, in the root of the problem, due to the weak form of multiple inheritance (multiple for interfaces only). The it is possible to have the situation where references of different interface types point to different place in memory — different places of the same runtime instance of some class.

And it all has nothing to do with shadowing.

—SA
 
Share this answer
 
Comments
Maciej Los 23-Feb-14 17:25pm    
I would say: it is closer to "inheritance" than to "shadowing" ;)
+5!
Sergey Alexandrovich Kryukov 23-Feb-14 18:17pm    
Thank you, Maciej.
Right. But by "shadowing", one could imply something also related to inheritance: when inherited member is shadowed by some new member with the same name, but this effect is 1) trivial, 2) more usually called "hiding".
OP did not ask anything about "shadowing", the question is answered...
—SA
vivektiwari97701 8-Apr-14 8:23am    
Thanx For The Reply. the question is not related to Shadowing .
Sergey Alexandrovich Kryukov 8-Apr-14 10:47am    
You are welcome. I mentioned "shadowing" just because you mentioned it. If you think that, nevertheless, the question is not related to shadowing, very good.

Will you accept the answer formally (green "Accept" button)?

—SA
Although it is really a FieldShadowing_2 underneath, the FieldShadowing_2 is pretending to be a FieldShadowing_1, which cannot display().

This is an example of inheritance and abstraction:
FieldShadowing_2 inherits FieldShadowing_1.
The variable b is an abstraction of FieldShadowing_2 because it is stored as a FieldShadowing_1.

Here is a rewritten version demonstrating more situations that do and don't have errors:

(I have changed the class names because FieldShadowing_1 is not a very descriptive class name, and because this code does not actually demonstrate shadowing, which is something else entirely!)

Java
public class A
{
    public String getName() { return "A"; }
}
public class B1 extends A
{
    public void display()
    {
        System.out.println("Check");
    }
}
public class B2 extends A
{
    public void doNothing()
    {

    }
}
public static void main(String[] args)
{
    B b1 = new B1();
    B b2 = new B2();
    A a1 = b1;
    A a2 = b2;

    System.out.println(b1.getName()); // OK: B1 has getName()
    System.out.println(b2.getName()); // OK: B2 has getName()    

    System.out.println(a1.getName()); // OK: A has getName()
    System.out.println(a2.getName()); // OK: A has getName()

    b1.display(); // OK:    B1 has display()    
    b2.display(); // ERROR: B2 does not have display()

    a1.display(); // ERROR: A does not have display()
    a2.display(); // ERROR: A does not have display()

    b1.doNothing(); // ERROR: B1 does not have doNothing()    
    b2.doNothing(); // OK:    B2 has doNothing()

    a1.doNothing(); // ERROR: A does not have doNothing()
    a2.doNothing(); // ERROR: A does not have doNothing()

    
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 23-Feb-14 14:10pm    
It is not "pretending". FieldShadowing_2 is actually FieldShadowing_1 at the same time.
And it can display! Only this is inadequate choose of compile-time type.
Correct, the OP's code is unrelated to shadowing.
—SA
Quote:
public class FieldShadowing_2 extends FieldShadowing_1

It means that FieldShadowing_2 has all the capabilities of FieldShadowing_1 but not the other way around!
By downcasting FieldShadowing_2 you also remove the extensions it adds...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Feb-14 14:09pm    
Nothing is "removed". It simply means that the runtime type is FieldShadowing_2, but the compile-time typ FieldShadowing_1 is used, which does not provide access to extension.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900