GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Parent/Child Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Parent/Child" Watch "Parent/Child" New topic
Author

Parent/Child

Mickey Atanasovic
Greenhorn

Joined: Jan 31, 2003
Posts: 2
class Parent {
int i = 100;
void Printme() {
System.out.println("Parent");
}
}
class Child extends Parent {
int i = 10;
void Printme() {
System.out.println("Child");
}
}
public class Class1 {
public static void main (String args[]) {
Parent p = new Child();
System.out.println(p.i);
p.Printme();
}
}
Output is :
100
Child
On which way I can access variable i of the Child class.
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Look for the answer here
Piotr Kaluski
Greenhorn

Joined: Jan 31, 2003
Posts: 8
In addition to an answer given by R Manivannan, it is worth noting than you can access i in Child class using explicit cast:
System.out.println(((Child)p).i);


Piotr Kaluski<br />SCJP 1.4
 
GeeCON Prague 2014
 
subject: Parent/Child