About Inheritance and Overriding Java
Why the "a" variable is null in "user.view" when i called "tulis" method from different object in program below ? Please help me :) import java.io.*; import java.util.InputMismatchException; class kelas1 { public String a; static BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); public void tulis () throws IOException{ a = "9"; } } class kelas2 extends kelas1 { @Override public void tulis () throws IOException{ a = "10"; } } public class tr extends kelas2 { public void view (){ System.out.println(a); } public static void main (String [] args) throws Exception{ tr user = new tr (); kelas1 obj = new kelas1 (); kelas2 obj2 = new kelas2 (); String in = input.readLine (); switch (in){ case "1" : obj.tulis(); user.view (); break; case "2" : obj2.tulis(); user.view (); break; } } }