• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Mock exam question

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Phone {
static String device = "Phone.device";
void showDevice() {
System.out.println("Phone.showDevice," + device + " ");
}
Phone() {
showDevice();
}
}

class Mobile extends Phone {
String device = "Mobile.device";
void showDevice() {
System.out.println("Mobile.showDevice," + device + " ");
}
Mobile() {
showDevice();
}
public static void main(String[] args) {
Mobile n = new Mobile();
n.showDevice();
}
}
Could you please explain how the answer came as
Mobile.showDevice,null
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device

Here null value also came.How?
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sukhavasi vasavi,

You better get it from polymaorphism.

if you want to break that use following:

Phone() {
this.showDevice();
}

this always referes to current object;
behold there will be two objects when you say new Mobile().

1. Phone
2. Mobile

Analyze and understand POLYMORPHISM.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasan thoyyeti:
if you want to break that use following:

Phone() {
this.showDevice();
}

This change has no effect on the output.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sukhavasi,

hava a look here.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have analyzed and corrected my mistake. We can't call parent class method with child class instance if its over-ridden in child. the way is in over-ridden method we can call "super();"

Interesting point is in Phone class "this" refer to "Mobile";

So we need to be careful with inheritance.
"this" may not always point to current class in which we call it.

Thanks Manfred Klug
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic