data-hiding

Are static variables inherited

泄露秘密 提交于 2019-11-27 01:56:34
I have read at 1000's of locations that Static variables are not inherited. But then how this code works fine? Parent.java public class Parent { static String str = "Parent"; } Child.java public class Child extends Parent { public static void main(String [] args) { System.out.println(Child.str); } } This code prints "Parent". Also read at few locations concept of data hiding. Parent.java public class Parent { static String str = "Parent"; } Child.java public class Child extends Parent { static String str = "Child"; public static void main(String [] args) { System.out.println(Child.str); } }

Encapsulation vs Data Hiding - Java

一个人想着一个人 提交于 2019-11-26 18:58:15
问题 Interviewer: What is encapsulation and how do you achieve it in Java? Me: Encapsulation is a mechanism to hide information from the client. The information may be data or implementation or algorithm. We achieve this using access modifiers. Interviewer: This is data hiding. How do we achieve encapsulation in Java? Me : uummmm Concrete Question: Other than 'Access Modifiers', what is the way to implement Encapsulation in Java? 回答1: More generally encapsulation refers simply to bundling the data