java - Having same property name in abstract class and its child class scenario -


i have abstract class:

public abstract class classparent{     protected integer id;     protected string message; } 

and child class:

public class classchild extends classparent{     private integer id;     private string message; } 

what happen in scenario? going inherit base class properties or give error on runtime? should getters , setters these properties?

subclass property hides parent class property. called shadowing , allowed in java. best avoid it. can verify following code sample

public class classchild extends classparent {     private integer id;     private string message;      classchild(integer id, string message) {         this.id = id;         this.message = message;     }      @override     public string tostring() {         return "classchild{" +                 "id=" + id +                 ", message='" + message + '\'' +                 '}';     }      public static void main(string[] args) {         classchild cs = new classchild(1, "test");          system.out.println(cs);         system.out.println(cs.message);         system.out.println(cs.id);         system.out.println(((classparent)cs).id);         system.out.println(((classparent)cs).message);      } }  

Comments