java - Confusion with Access Modifiers while using in inheritance -
i have been working on code , not able figure out why access modifiers work , not:
public class base of operations { protected int method(int x) { homecoming 0; } } class kid extends base of operations { // line 1: compiles public int method(int x) { homecoming 0; } // line 2: cannot cut down visibility of inherited method base of operations private int method(int x) { homecoming 0; } // line 3: compiles private int method(long x) { homecoming 0; } // line 4: homecoming type incompatible base.method(int) protected long method(int x) { homecoming 0; } // line 5: compiles protected int method(long x) { homecoming 0; } // line 6: compiles protected long method(long x) { homecoming 0; } // line 7: compiles protected long method(int x, int y) { homecoming 0; } } i trying understand why lines 1, 3, 5, 6, , 7 allowed lines 2 & 4 not.
i've tried searching haven't found clear explanation, though did find in java, what's difference between public, default, protected, , private?
the methods taking long or 2 ints argument (3,5,6,7) not overriding blip method method signatures (combination of name , parameters) different.
line 1 fine because increases visibility of method. inversely, line 2 failing because decreases visibility of method.
line 4 not working because homecoming value different, , not in more specific way. when returning primitives, homecoming value cannot change. however, when returning objects, possible subclasses homecoming more specific class. example, cloneable.clone() returns object. actual implementations free alter homecoming type of object.
java method-overloading method-overriding
No comments:
Post a Comment