• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Properties cannot be Overridden?

 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods can be Overridden... But why this partiality to Properties?... Why Properties or instance variables cannot be overridden?.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would overriding an instance variable mean? I just don't think it would make sense. There's no "implementation" to override. If an instance variable is private then subclasses can't access it anyway, and if it isn't subclasses can just use it.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If methods are declared private, methods also cannot be overridden... Could you please elaborate the reason?...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:If methods are declared private, methods also cannot be overridden... Could you please elaborate the reason?...


If a method is private, the subclass doesn't know anything about it. As far as any class other than the class it's defined in is concerned, it doesn't exist. So it can't be overridden.

A subclass can define a method with exactly the same name and signature if it wants. But then that just behaves like a completely separate method.

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the Java language designers let us overridden the instance variable then the instance variable need to be declared with any access modifier rather than private which will break a fundamental principle of object oriented programming, i.e., encapsulation. But, we can override the properties method in the subclasses as follows:
 
reply
    Bookmark Topic Watch Topic
  • New Topic