0

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; } } }

21st Sep 2018, 11:05 AM
Dimas Chandra
Dimas Chandra - avatar
1 Respuesta
0
inheritance is a mechanism that allows a new class to be created from an existing class
21st Sep 2018, 2:34 PM
Sunil Chauhan
Sunil Chauhan - avatar