• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parent/Child

 
Mickey Atanasovic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look for the answer here
 
Piotr Kaluski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic