public class A {
public A() {
foo();
}
private void foo() {
System.out.print("A::foo ");
goo();
}
public void goo() {
System.out.print("A::goo ");
}
}
public class B extends A {
public B() {
foo();
}
public void foo() {
System.out.print("B::foo ");
}
public void goo() {
System.out.print("B::goo ");
}
public static void main(String[] args) {
A b = new B() {
public void foo() {System.out.print("Anonymous::foo ");}
public void goo() {((B)this).foo();}
};
}
}
I'd like your help with understanding why does the program print A::foo Anonymous::foo Anonymous::foo. Is this anonymous class replace the former B? overrides its methods?
As I see it, it should go to A's default constructor, run A's foo- print "A::foo", than run B's goo, since it was properly overrided, but now B's goo is the one in the Anonymous class, so it casts this to B (Which does nothing), and run its foo, which is the foo above, of B, so it should print "Anonymous:foo". What do I get wrong?
Thanks a lot.
Your question isn't all that clear, but let me just say that the the answer would be exactly the same if instead of an anonymous class extending B, you had a top-level class C extending B. Nothing about anonymous classes makes them behave differently with respect to polymorphism and inheritance. When B's constructor calls foo(), the overriding version in the most-derived class -- here the anonymous class -- is invoked.
I think the confusing thing here is you have two foo methods. One is private so it's not eligible for overriding, the other is public so it can be overridden. B is calling foo in its constructor but that's overridden by its subclass.
A's constuctor calls A.foo (A::foo) because it is private and so not overloaded. A.foo calls goo() which was overridden by B and then by Anonymous so you get Anonymous.goo -> Anonymous.foo (Anonymous::foo). Then B's constructor calls foo which is overridden by Anonymous so (Anonymous::foo)
Using that kind of anonymous construction in fact creates a subclass of B. You have overridden B's methods with the ones you provide in the anonymous class so those will be used instead.
Related
The output of the program is A isn't it suppose to be B. If I change the modifier of method in Class A to public then the output is B. Can somebody explain what is going on here?
Code:
public class HelloWorld {
public static void main(String[] args) {
HelloWorld hw = new HelloWorld();
hw.createInstance();
}
public void createInstance() {
A b = new B();
b.isTrue();
}
public class A {
private void isTrue() {
System.out.println("A");
}
}
public class B extends A {
public void isTrue() {
System.out.println("B");
}
}
}
Output: A
If isTrue() in A was public, then isTrue() in B would override it. (Overriding means if you call a method declared in the base class, the matching method in the subclass is executed.)
In this case, since isTrue() in A is private, the two isTrue() methods are independent. Private methods are not subject to overriding.
Since you are calling isTrue() on a variable of type A, it is the isTrue() method in A that is executed. Being private does not, in this case, prevent you accessing the method, because all your code is inside one class, HelloWorld. If your classes were not inner (or nested) classes, then you wouldn't be able to call the private method from outside the class it is declared in.
You are creating a B instance, pointed by an A Instance.
As A isTrue is private, the compiler will raise an error
If you make A.isTrue() public, as you created a B instance it will execute B isTrue method, as that method is overridden
Due your classes are inner classes, when you call isTrue pointed by A class it executes isTrue from A and does not apply the override because the methods have different visibility. When you make isTrue from A public it applies polymorphism as they have the same modifier.
The confussion is because they are inner classes, if they weren't inner classes your code won't compile
I was asked this question in a recent interview. Looking to get some help.
Class A has foo() method triggered from constructor.
public class A {
public A() {
foo();
}
public void foo() {
System.out.println("In foo method in class A");
}
}
Class B overrides the foo method in class A.
public class B extends A {
#Override
public void foo() {
System.out.println("In foo method in class B");
}
}
Now if we create instance of class B the foo() method in B will be called.
A a = new B();
prints: "In foo method in class B"
Question: Lets say we own the class A and it is part of a jar file(abc.jar) and how do we make sure when class B is instantiated A.foo() is called instead of overridden B.foo()?
Conditions:
Imagine the jar is shared to other users and we cannot break client code my marking the method private/final.
Also calling super.foo() from class B is also not an option since we don't own class B and cannot restrict users.
public class A {
public A() {
fooInternal();
}
public void foo() {
fooInternal();
}
private final void fooInternal() {
System.out.println("In foo method in class A");
}
}
You can't make it invoke A.foo(), because that method is overridden. You can only make it invoke a method that A.foo() invokes, and that can't be overridden.
The more important point here is that you shouldn't ever invoke overrideable methods from a constructor.
Mark A's foo method as final. It is the only way.
In order to still allow B to also get a ping on initialization, the solution is a two-stage construct: A's constructor invokes the final foo() method, but as part of the foo() method, foo2, or subfoo, or whatever you want to call it, is also invoked, and that method is defined in A as a noop (does nothing).
Generally such a final init()-style method should also be private. Based on common logic: What are the odds that an 'init' operation is also a concept that external code could plausibly want to invoke a second time at some arbitrary later point in time? Highly unlikely, which is why it should be private. Once you do so, well, private methods are effectively inherently final, so that takes care of that:
class A {
public A() {
init0();
}
private final init0() {
// do stuff here - subclasses won't stop you.
init();
}
// B can override this if it wants.
protected void init() {}
}
In the book Java: The complete reference
// Demonstrate when constructors are called.
// Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
// Create a subclass by extending class A.
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
// Create another subclass by extending B.
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}
Output:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor
It is demonstrating how the constructor of a subclass is called. But why are constructors of the super class called in the absence of a super() constructor.
Why did the Java Language designers consider it necessary to do so?
As others have pointed out, if you don't start your constructor with a super(...) call, the compiler will put in a call to super() for you.
As to the why, you have to start with remembering what a constructor is for in the first place: initializing the object. What does that mean, specifically? In practice, it means assigning values to the object's fields, and establishing invariants.
Without a call to super(), the B and A classes wouldn't have a chance to do that for whatever fields they contain. And you can't even have the C() constructor do it for them, if those fields are private, since private fields aren't accessible outside your class (not even your super class's fields are accessible). Even if you could, it wouldn't be a good idea; it would also break encapsulation. For instance, imagine having to change your code if a super class -- possibly a complex one whose internals you're not an expert in -- suddenly decided to change its implementation details.
To illustrate this, consider a very simple set of classes:
public class Super {
private final String name;
Super() {
name = "default";
}
public String name() {
return name.toUpperCase();
}
}
public class Sub extends Super {
public Sub() {
// don't do anything
}
}
When you instantiate Sub, it will start out by calling Super's constructor. If it didn't, the name field would be null (the default value for reference types). But the name() method doesn't check for null; it assumes that the reference is non-null, because the constructor establishes that invariant. So, in our pseudo-Java that doesn't call the super constructor, Super.name has to get a bit more complicated -- it has to check for name == null.
You can imagine that as the classes gain more fields, with more interesting invariants, this toy example can become more and more complicated. Forcing you to call the super constructor -- either explicitly or implicitly -- lets the authors of that super class establish their invariants, resulting in simpler, more maintainable code.
Every constructor calls its superclass constructor. super() call take place as the first line in the constructor. From javadoc:
If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the
no-argument constructor of the superclass. If the super class does not
have a no-argument constructor, you will get a compile-time error.
Object does have such a constructor, so if Object is the only
superclass, there is no problem.
more here
Because it says so in the Java Language Specification.
If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.
Even it has a role with Abstract classes also. we can't initialize object of abstract class. But Child class of Abstract class calls the super() method by default. So abstract class constructor can initialize its instance variables.
for example:
public abstract class TestA {
private int a;
public TestA()
{
a=10;
}
public int displayA()
{
return a;
}
abstract void display();
}
public class TestB extends TestA{
#Override
void display() {
System.out.println("this is class B");
}
}
package Abstract;
public class TestMain {
public static void main(String[] args) {
TestA obj= new TestB();
System.out.println(obj.displayA());
}
}
Output is : 10
Here you can see, when we initiating object of class TestB , by default super constructor is calling and TestA's constructor is assigning the value of a. If super will not be called by default we can't assign instance variables of abstract class.
Inheritance is basically inheriting all the properties of your parent class. So if a sub class constructor is called, it should definitely and by default inherit all its parent class properties also. In the following code, all the properties of class A should be made available in class B also, so if I just call B's constructor, all the class A's properties(except private) are also initialized and made available, meaning B has inherited A's properties
class A {
protected int a;
A() {
a=12;
System.out.println("Inside A's constructor.");
}
}
class B extends A {
B() {
System.out.println("Inside B's constructor.");
System.out.println(a);
}
}
public class ConstructorInheritance {
public static void main(String args[]) {
B b=new B();
}
}
output:
Inside A's constructor.
Inside B's constructor.
12
Imagine class C accessing an unitialized variable of class B or A. Implicitly calling constructors of class B-->class A makes sure that you are always accessing initialized variables of inherited classes(A or B)
"The Java programming language" says "A constructor in subclass can initialize its individual state, however, as keeping contract, only super class knows how to initialize super class's state".
Thus, constructor of super class have to be called. There is sequence how the constructor processed:
Call super class constructor
Initialize fields with initializers and initialization blocks
Execute body of the constructor
For more details, have a look of the book to section "3.2".
We can not override a private method then why does the followng code does not give an error. Instead it produces the output.
class A {
private void fun() {
System.out.println("ths is a private method");
}
}
class B extends A {
void fun() {
System.out.println("ths is wrng");
}
}
class C {
public static void main(String args[]) {
B ob = new B();
ob.fun();
}
}
private methods are not inherited. In your main method you're invoking the fun() method on a variable of type B. The fun() method of type B seems to be accessible, assuming your class B and class C are in the same package.
Had you done this
A ob = new B();
ob.fun();
Then you would have gotten your compilation error.
B#fun() is completely unrelated to A#fun().
Both are completely different methods and not related to each other. As private methods are not inherited there is no concept of overriding here.
Method fun() is not overridden in B class because it was never inherited from class A.
What you are invoking in your code is method of B class and that has no relation with class A and method A#fun().
To check this, Add a #Override annotation and you will get compile time error.
// Compilation error.
Class B extends A{
#Override
void fun(){
System.out.println("ths is wrng");
}
}
Now this code will not compile.
First of all, private methods are not inherited in child classes. If you still try to write a method with same name as that of parent class, java compiler will consider the method in child class as a completely new method and will not throw any exception. Its like the new method in child class shadows the private method in parent class.
class A
{
public void m1()
{
System.out.println("hi-base class");
}
}
class B extends A
{
public void m1()
{
System.out.println("hi-derived ");
}
public static void main(String args[])
{
B b1=new B();
}
}
In this i want to invoke base class m1 method by using Derived class object without using the super
You would need to construct an object of type A. You have overridden method m1 in the derived class, and so any calls to m1 on an object that was created as a B will have the B version of m1 invoked. Without using super, there's no way to instruct the compiler to make the non-virtual call to the base-class version.
Are you just looking for super.m1();? This will invoke the immediate parent's method.
However, you cannot instantiate an object of type B from outside of B and use this.
You cannot do:
B value = new B();
value.super.m1(); // call A's implementation
However, you could do this within B:
#Override
public void m1()
{
System.out.println("hi from B");
super.m1();
}
public void useAM1()
{
super.m1();
}
Of course, when you start to provide workarounds to get at functionality from A, then it sounds like you are abusing inheritance, or at least should have used an instance of A to begin with.
Interestingly, in C++ you could do this: value->A::m1();. Fortunately, there is no equivalent in Java.
In short you cannot do it -- virtual dispatching would delegate the call to the referred.