java - Override attributes -



java - Override attributes -

when override method must maintain method's signature , can't cut down visibility. tried happends when attributes. surprised - works! yourself:

public class { public integer myattribute; } public class b extends { public string myattribute; } public class main { public static void main(string[] args) { b b = new b(); b.myattribute = "mystring"; ((a) b).myattribute = 1337; system.out.println(b.myattribute); system.out.println(((a)b).myattribute); } }

so possible write attribute in subclass same attibute name in superclass, can utilize different visibility (modifier) , type. attributes in superclass , subclass independend each other.

now if utilize same attribute name in superclass , subclass hide superclass's attribute. when accessing attribute using subclass, subclass's attribute. superclass's attribute there, too! have perform cast access superclass's attribute outside. within "super" keyword should usefull.

question 1: have 2 different attributes same name. doesn't break lsp?

why came detail this: experimenting self written persistence framework. want read finish internal state of object , persist state. read attribute's values via reflexion, starting @ subtype , traversing on superclasses. if there 2 attributes same name in superclass , subclass have remember class declares attribute able map attributes values right attributes , restore object's state.

question 2: other ideas how deal detail? question 3: how other persistence frameworks handle detail?

i never saw detail in use. maybe writing 2 attributes same name bit ugly, possible , persistence framework may confronted it. maybe there situations technique might usefull.

thans in advance.

orms utilize javabeans standard defines "properties". properties defined reader and/or author method rather field read/write. in 99% of cases properties field+getter&setter, need not case. , orm reads these properties, , detects fields have getters&setters. since methods overridden rather shadowed, problem gone.

this more problem of encapsulation violation of lsp. field access not affected polymorphism, if have foo foo = new foosubclas();foo.fieldthe value of thefoo` field taken, means behaviour won't change.

java inheritance attributes override

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -