Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Core Java Problem - Multilevel inheritance ,variable access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Core Java Problem - Multilevel inheritance ,variable access" Watch "Core Java Problem - Multilevel inheritance ,variable access" New topic
Author

Core Java Problem - Multilevel inheritance ,variable access

Amol Pingate
Greenhorn

Joined: Oct 02, 2008
Posts: 14
public class A
{
int i=10;
}


public class B extends A
{
int i=20;
}

public class C extends B
{
int i=30;

public void display()
{
System.out.println(this.i); // This line will print 30
System.out.println(super.i); // This line will print 20

System.out.println(); // i want to access int i of class A
}
public static void main(String args[])
{
C objc = new C();
objc.display();
}
}



How to access variable "i" of class A , in display method of class C.


Please can anyone tell me.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3060
    
  38

Casting would be helpful

System.out.println(((A)this).i);


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Amol Pingate
Greenhorn

Joined: Oct 02, 2008
Posts: 14
Thank you

it's working...........
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Welcome to JavaRanch.

The Sun Certification Results forum is meant for people to post messages when they have done a Sun certification exam - it is not for asking questions about Java. I will move this topic to a more appropriate forum for you. Next time, please select the appropriate forum yourself.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Gopi Arumugam
Greenhorn

Joined: Mar 05, 2007
Posts: 10
Hi dude, if you not fix with this problem Multilevel Inheritance

If you see this video which clearly explain you about the concept of "Multilevel Inheritance" in Java

http://www.youtube.com/watch?v=MA7-r83gat4

All the Best!!!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

gopivista wrote:


Please check your private messages for an important administrative matter.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Amol please Use Code Tags when you post a source code. You can edit your message using this button ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Core Java Problem - Multilevel inheritance ,variable access