INHERITANCE AND MEMBER ACCESSIBILITY What? - which data and methods can be accessed from where How? - rules which apply equally to all members, data or methods, class or instance Example Hierarchy: Grandparent Mother Uncle Child Each class has one data members - public - named with first letter of class and type, e.g. g_pub is Grandparent's data member What? - inheriting public How? - always inherited from ancestors Examples: - Draw class diagrams, public data only - What's legal? public void childMethod() { g_pub = "cg"; => yes m_pub = "cm"; => yes c_pub = "cc"; => yes u_pub = "cu"; => no } public void momMethod() { g_pub = "mg"; => yes m_pub = "mm"; => yes c_pub = "mc"; => no u_pub = "mu"; => no } public void uncMethod() { g_pub = "ug"; => yes m_pub = "um"; => no c_pub = "uc"; => no u_pub = "uu"; => yes } public void grandMethod() { g_pub = "gg"; => yes m_pub = "gm"; => no c_pub = "cm"; => no u_pub = "cu"; => no } What? - accessing public How? - accessible from any object or class, regardless of inheritance Example: - If it's always accessible, why did we have problems above? => because we were treating it as our own data; always accessible means it can be used directly when associated with a class or instance (depending on its type) Example: grandMethod() { Child gc = new Child(); gc.c_pub = "gc"; Mother gm = new Mother(); gm.m_pub = "gm"; Uncle gu = new Uncle(); gu.u_pub = "gu"; Grandparent gg = new Grandparent(); gg.u_pub = "gg"; } - How would we change this for childMethod? momMethod? uncleMethod? => They can all be exactly the same. - Draw a chart What? - inheriting private How? - not inherited Example: - Add private data to our class diagrams, similar naming scheme (using pri) - What's legal? public void childMethod() { g_pri = "cg"; => no m_pri = "cm"; => no c_pri = "cc"; => yes u_pri = "cu"; => no } public void momMethod() { g_pri = "mg"; => no m_pri = "mm"; => yes c_pri = "mc"; => no u_pri = "mu"; => no } public void uncMethod() { g_pri = "ug"; => no m_pri = "um"; => no c_pri = "uc"; => no u_pri = "uu"; => yes } public void grandMethod() { g_pri = "gg"; => yes m_pri = "gm"; => no c_pri = "cm"; => no u_pri = "cu"; => no } What? - accessing private How? - never accessible from any outside object or class Example: childMethod() { Mother cm = new Mother(); cm.setPriv("gm"); Uncle cu = new Uncle(); cu.setPriv("cu"); Grandparent cg = new Grandparent(); cg.setPriv("gg"); Child cc = new Child(); cc.c_priv = "gc"; } - How would this change for the others? => Draw a chart. What? - inheriting protected What? - always inherited by people who directly or indirectly extend you Example: - Add protected data to our class diagrams, similar naming scheme (using pro) - What's legal? public void childMethod() { g_pro = "cg"; => yes m_pro = "cm"; => yes c_pro = "cc"; => yes u_pro = "cu"; => no } public void momMethod() { g_pro = "mg"; => yes m_pro = "mm"; => yes c_pro = "mc"; => no u_pro = "mu"; => no } public void uncMethod() { g_pro = "ug"; => yes m_pro = "um"; => no c_pro = "uc"; => no u_pro = "uu"; => yes } public void grandMethod() { g_pro = "gg"; => yes m_pro = "gm"; => no c_pro = "cm"; => no u_pro = "cu"; => no } What? - accessing protected How? - a method can only access members that - belong to its own same class - belong to its ancestor classes Example: momMethod() { Grandparent mg = new Grandparent(); mg.g_pro = "gg"; Uncle mu = new Uncle(); mu.setPriv("mu"); Mother mm = new Mother(); mm.m_pro = "gm"; Child mc = new Child(); mc.setPriv("mc"); } - How would this change for the others? => Draw a chart What? - default visibility How? - package: component is accessible from any method of a class that belongs to the same package as the component's class - not used in this course