This question already has answers here:
“overriding” private methods with upcasting call in java
(1 answer)
Overriding private methods in Java
(10 answers)
Closed 5 years ago.
Can anybody explain why output to below question is "A.test" ?
class A {
private void test(){
System.out.println("A.test");
}
public void mytest(){
this.test();
}
}
class B extends A{
protected void test(){
System.out.println("B.test");
}
}
public class Test{
public static void main(String[] args) {
A a = new B();
a.mytest();
}
}
The test() method of class A cannot be overridden by class B, since it is private. Therefore this.test(); invokes A's test() method even though it is executed on an instance of class B.
Super class can invoke the methods of the sub class without using typecasting, without using reflection and without using a reference other than this only if they are overridden.
In your case A a = new B(); , object of B is created which has both the behaviours private void test() as inherited by super class A as well as protected void test() as added by B. The reason for both the methods being there and not just one is that due to the acess modifier being private the method is not visible in subclass scope. Hence it can not overriden and adding the method with same name just simply adds another method.
As there is no run time polymorphism in this case hence compile time target method is resolved and method defined in A is invoked.
If in case you would have overriden the mytest in B and from the overriden mytest you would have made a call to test then method in B would have been invoked. This is easy to understand as any method of B can not see any private method of its super class.
Related
This question already has answers here:
Why java.lang.Object can not be cloned?
(10 answers)
Closed 4 months ago.
Object's clone method is protected, therefore it can be accessed in sub classes (class A), so why am I getting 'clone() has protected access in java.lang.Object' compiler error? I thought, that all Java classes are sub classes of Object. Thanks in advance.
The code below raises the compiler error:
public class A {
public static void main(String[] args) {
Object o = new Object();
o.clone();//error
}
}
But this one compiles perfectly, don't they have the same semantics tho?
public class A {
protected void foo() {
}
}
public class B extends A {
public static void main(String[] args) {
A a = new A();
a.foo();
}
}
No, they don't.
protected means 2 things:
It's like package, _and that explains why your second snippet can call foo(). It's not about the fact that B extends A, it's that A is in the same package as B.
Subclasses can invoke it.. on themselves only. Trivially (but this doesn't work if its final), you can simply override it, implement it as return super.clone(); and now you can call it just fine.
protected members can be accessed anywhere in the same package and outside package only in its child class and using the child class reference variable only, not on the reference variable of the parent class. We cant access protected members using the parent class reference.
This question already has answers here:
Polymorphism and Constructors
(2 answers)
Closed 3 years ago.
I have a derived Java class override a base class' method. When the base class calls the method, it executes the derived class' function, rather than its own. Why?
public class HelloWorld{
public static void main(String []args){
Derived d = new Derived();
System.out.println("Main");
}
}
class Base {
void f() {
System.out.println("Base::f()");
}
public Base() {
f();
}
}
class Derived extends Base {
void f() {
System.out.println("Derived::f()");
}
public Derived() {
f();
}
}
The code prints Derived::f() twice, I expect it to print Base::f() followed by Derived::f(), as would happen in C++
In Java, unlike in C++, instance methods are virtual by default. It means that a method call is dispatched at the run time according to the actual run-time class of an object (not at the compile time). In C++, you achieve this behaviour with keyword virtual.
Your Derived f() method is overriding the Base f() method. If you want to see the Base method try putting super.f(); in the start of the Derived f() method, this will call the super class' f() method.
This question already has answers here:
private method in inheritance in Java
(6 answers)
Closed 4 years ago.
// file name: Main.java
class Base {
private void foo() {
System.out.printf("Message");
}
}
class Derived extends Base {
public void foo() {
System.out.printf("Message1");
} // works fine
}
public class Main {
public static void main(String args[]) {
Base d = new Derived();
d.foo();
}
}
when I write Derived d=new Derived() it works fine but when i write Base d =new Derived() it gives error of private function cannot be overrided.
I don't know why you say "It gives error of private fn cannot be overrided" - that's not the error you get when you try to compile this. It gives the following error instead:
$ javac Main.java
Main.java:16: error: foo() has private access in Base
d.foo();
^
1 error
Why: Because foo() is a private method in class Base, so you cannot call it on a variable of type Base.
Also: You can indeed not override private methods; in fact, the foo() method in class Derived is a method that is completely separate from the foo() method in class Base, that just happens to have the same name. It does not override the method in class Base.
This is because foo is private in Base class. You can use public or protected
The method you want to inherit is private. So you don't override it. You hide it instead. So Base.foo and Derived.foo are completely different methods.
Note: hiding private methods is valid
I would suggest you to use #Override annotation on methods you override. It will prevent you from hiding private methods.
This question already has answers here:
Confusing "override a private method"
(3 answers)
Can I override a private method in Java?
(10 answers)
Java private method override
(4 answers)
Override "private" method in java
(5 answers)
Closed 4 years ago.
class Base {
private void func(){ // method overridden if public
System.out.println("In base func method");
};
public void func2() {
System.out.println("func2");
func(); // alternatively, this could be: ((Derived) this).func();
}
}
class Derived extends Base {
public void func(){
System.out.println("In Derived Class func method");
}
}
class Test {
public static void main(String [] args) {
Derived d = new Derived();
d.func2();
}
}
As written, this code will call the func() of the Base class because this method is private and there is no method overriding going on.
However, if the func() line of func2() is changed to ((Derived) this).func(), the func() of the Derived class will be called.
From what I can tell, it seems as if the Derived object is being treated like a Base object once the control enters the func2() of the Base class.
Is my understanding correct? How can my understanding be reconciled, if at all, with method overriding in the case where the func() of the Base class is instead public rather than private and runtime binding occurs? Does the call to func() in func2() first find the Base class func() and then decide to use func() in the subclass due to method overriding? Exactly what happens at runtime here?
According to what I have understood from your question, you are confused about what happens during the runtime when the method func() is called from func2() of the Base class.
When Java finds a method inside the superclass's method that is overridden in the subclass, it finds that the method is in the subclass.
So, in your case, the method func() is executed, not of the Base class but of the Derived class due to late binding. Because the Derived Object is calling the method func2(), the method func() comes back to the subclass (Derived). If, however, the call were made to the Base class, the func of the Base class would be called.
So the overall takeaway is this: Late binding is purely based off of the object calling the function. Java checks for overridden methods of the subclass during the late binding (run-time).
This question already has answers here:
Private methods in Inheritance
(9 answers)
Closed 7 years ago.
A friend of mine asked this question to me. Why the following code does not give error on invoking aa.x()?
I understand that aa is a reference to object of class B but is invoking private method of class A inside the method of class A where it is visible and hence accessible.
Is my understanding correct? Or is there any other reason behind this?
public class A {
public void xyz() {
System.out.println("A");
}
private void x() {
System.out.println("A:x");
}
public static void main(String[] args) {
B b = new B();
A aa = b;
aa.x();
aa.xyz();
B bb = (B) aa;
bb.xyz();
bb.xyz12();
}
}
class B extends A {
public void xyz() {
System.out.println("B");
}
public void xyz12() {
System.out.println("B-12");
}
}
I can't immediately find a duplicate using a subclass, but fundamentally it's the same answer as the answer to this question.
There are two things that govern access to x:
Where the code is that's doing the access. Since x is private to A, the code accessing it must be part of a method in A. It can't be in a subclass (B) or an unrelated class.
What kind of reference you're using. If you have an A reference, you can access x on it. If you have a B reference, you can't, even though your code is part of an A method. You could cast it to A and then access x, but you can't do it directly with a reference of type B.
It is only visible because the main method where it is invoked is contained in class A. Move it to class B and it will not work
As the private methods are not inherited, a superclass reference calls its own private method.
Your main method is the method of A, therefore it can can call x() private method.
private modifier—the field is accessible only within its own class.