• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between data hiding and overriding  RSS feed

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we can access method of Base and its data member same way then why one is data hiding and other is method overriding
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What results do you get?
Because that second call to show() in main results in a call to Child'd show() method (as expected).
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO !!! .IT RUNS method of Base class
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sachin,

I have corrected code indentation of your original code, I hope you're fine with that.
Now is the way easier to read your code.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by putting some additional print statements to show what you are calling. That shows that the output from the uncast version and the version with the cast are the same.
The problem you have is that you are not hiding data. If you are working inside the same package, then there is no difference from public access. If you want data hiding, you should give the field private access.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:NO !!! .IT RUNS method of Base class
I have just seen that it doesn't. It is polymorphic and calls the subclass version with or without the cast.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not noticed that.Actually i was thinking by the second call in main i m still able to execute Base class function...
I am really sorry for my silly mistake
 
Chris Vetek
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am taking the java 1 test tomorrow. based on what i have been studying, it prints the child method because methods are runtime and variables are compile time. that is why the last output is 9 from base, but even though you have explicit cast to the base for the second call, its still the child method called.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Chris Vetek.
 
Chris Vetek
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Passed my IZ0-803 Java OCA Programmer 1 test today!
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Vetek wrote: . . . methods are runtime and variables are compile time. . . .
Instance methods are bound at runtime; static methods are statically bound (hence the word static) at compile time.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!