Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritence

 
paramasivam Ramu
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Explain me below code.


class Phone {
String device = "Phone.device";
void showDevice() {
System.out.println("Phone.showDevice," + device + " ");
}
Phone() {
System.out.println("Phone construtor");
showDevice();
System.out.println("Phone construtor end");
}
}

class Mobile extends Phone {
String device = "Mobile.device";
void showDevice() {
System.out.println("Mobile.showDevice," + device + " ");
}
Mobile() {
showDevice();
}
public static void main(String[] args) {
Phone n = new Mobile();
n.showDevice();
}
}

output is :

Phone construtor
Mobile.showDevice,null//why Mobile device is called?why it prints null?
Phone construtor end
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device
 
Mohana Priya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic