I am good at created a little block diagram; however, I struggle with tracing these out and I have a midterm in a few hours. I am confused about all the casting and also about #4 and #7.

I thought #4 would be a compiler error because the "Decared Type" (Three) doesnt have m1.
I also thought the output for #7 would be Three2. Why is Four1/One1 printed when only method2 is called. Thank you!!!

CSE143 Inheritance Example		handout #15

Assuming that the following classes have been defined:

	public class One {
	    public void method1() {
		System.out.println("One1");
	    }
	}
	                            
	public class Two extends One {
	    public void method3() {
		System.out.println("Two3");
	    }
	}
	
	public class Three extends One {
	    public void method2() {
		System.out.println("Three2");
		method1();
	    }
	}
	
	public class Four extends Three {
	    public void method1() {
		System.out.println("Four1");
		super.method1();
	    }
	
	    public void method3() {
		System.out.println("Four3");
	    }
	}
	
And assuming the following variables have been defined:

	    One var1 = new Two();
	    One var2 = new Three();
	    One var3 = new Four();
	    Three var4 = new Four();
	    Object var5 = new Three();
	    Object var6 = new One();

In the table below, indicate in the right-hand column the output produced by
the statement in the left-hand column.  If the statement produces more than one
line of output, indicate the line breaks with slashes as in "a/b/c" to indicate
three lines of output with "a" followed by "b" followed by "c".  If the
statement causes an error, fill in the right-hand column with either the phrase
"compiler error" or "runtime error" to indicate when the error would be
detected.

        Statement                       Output
        ------------------------------------------------------------

	var1.method1();			____________________________

	var2.method1();			____________________________

	var3.method1();			____________________________

	var4.method1();			____________________________

	var5.method1();			____________________________

	var6.method1();			____________________________

	var4.method2();			____________________________

	var4.method3();			____________________________

	((Two)var1).method2();		____________________________

	((Three)var1).method2();	____________________________

	((Two)var1).method3();	        ____________________________

	((Four)var2).method1();		____________________________

	((Four)var3).method1();		____________________________

	((Four)var4).method3();		____________________________

	((One)var5).method1();		____________________________

	((Four)var5).method2();		____________________________

	((Three)var5).method2();	____________________________

	((One)var6).method1();		____________________________

	((One)var6).method2();		____________________________

	((Two)var6).method3();		____________________________


		    Solution to CSE143 Inheritance Example

1. Inheritance Mystery.  The output is as follows.

        Statement				Output
	--------------------------------------------------
	var1.method1();				One1
	var2.method1();				One1
	var3.method1();				Four1/One1
	var4.method1();				Four1/One1
	var5.method1();				compiler error
	var6.method1();				compiler error
	var4.method2();				Three2/Four1/One1
	var4.method3();				compiler error
	((Two)var1).method2();			compiler error
	((Three)var1).method2();		runtime error
	((Two)var1).method3();			Two3
	((Four)var2).method1();			runtime error
	((Four)var3).method1();			Four1/One1
	((Four)var4).method3();			Four3
	((One)var5).method1();			One1
	((Four)var5).method2();			runtime error
	((Three)var5).method2();		Three2/One1
	((One)var6).method1();			One1
	((One)var6).method2();			compiler error
	((Two)var6).method3();			runtime error

Recommended Answers

All 11 Replies

Quick reading about inheritance here. Now, let's review #4:

var4.method1();

When class method1 in class Four is defined:

public void method1()
{
   System.out.println("Four1");
   super.method1();
}

Meaning that first a line will be printed "Four1" and then the super.method1() will be invoked. You are right, you cannot see method1() inside class Three, the parent of class Four - but class Three is inheriting from class One, and class One does have method1()!
The rules are very simple - every class is inheriting all the non-private methods and members from all the ancestors - meaning from the direct parent, its parent and so on. If a class defines a member and/or a method with the same name as one of the parent's members and/or methods - it is said the the child is overriding this member or method, and the by that each call to the name of the member or method will be the newly defined one. That is why when class Four is calling method1(), it is calling its own method1() that you have seen in the code. Since class Three did not override method1(), it has the original method1() defined by its parent, class One. Therefore, the call to super.method1() actually calls the class Three has from class One:

public void method1() 
{
   System.out.println("One1");
}

And the solution for this problem will be "Four1" followed by a new line and "One1".

Now, following the same logic - can you solve the problem you had with #7?

Thanks very much for taking the time to reply. That kind of makes sense to me. Could you go over the casting ones at the bottom please?

Actually, after looking at #7... i still dont understand why the answer is Three2/Four1/One1 because only m2 is called.

When I draw these out.... One is at the top.. then Three is connected to One, Four is connected to three. Does Four have everything Three and One has... except what it's overrides.. or only everything One has?

Sure, let's review the first one:

One var1 = new Two();
((Two)var1).method2();

Where class Two is defined:

public class Two extends One 
{
   public void method3() 
   {
   System.out.println("Two3");
   }
}

method2() does not exist in class Two nor in its parent class One, and the compiler will return an error that the method does not exist.
On the other hand, the second problem:

((Three)var1).method2();

Will pass compilation because indeed class Three has method2(). The problem is, that at runtime, when the command will be actually invoked, there will be no method2() since var1 is actually

One var1 = new Two();

And not Three as the casting claims - there is no method2() to call, and because of the casting it will be caught only at runtime.

Try to go over the other cases with that logic, and if you encounter any more problems post them here.

Actually, after looking at #7... i still dont understand why the answer is Three2/Four1/One1 because only m2 is called.

When I draw these out.... One is at the top.. then Three is connected to One, Four is connected to three. Does Four have everything Three and One has... except what it's overrides.. or only everything One has?

Number 7 is a bit more tricky:

Three var4 = new Four();
var4.method2();

method2() is not specifically written in class Four, so as seen before class Four inherits it from class Three:

public class Three extends One 
{
    public void method2() 
    { 
	System.out.println("Three2");
	method1();
    }
}

So the first line that will be printed is "Three2" and then method1() will be invoked. Now here is the trick - var1 is actually defined as new Four()! which means that the method1() that will be invoked is the one that belongs to class Four (quick reading about polymorphism here)

public void method1() 
{
   System.out.println("Four1");
   super.method1();
}

Which will print on the second line "Four1" and then will invoke super.method1() - Four's parent is Three, so Three's method1() will be invoked, which means as we explained before since Three doesn't have method1() written, it will inherit the method from its parent class One:

public void method1() 
{
   System.out.println("One1");
}

Which will print a third line "One1". Overall we will have "Three2/Four1/One1".

var4.method2();

So, I look at var4 and see that: Three var 4 = new Four();

Four doesn't have method 2, but 3 does. So "Three2" is printed. That makes sense to me; however... why is method 1 (which super's m1) of Four printed, resulting in Three2/Four1/One1. I dont understand how or why method 1 is called.

Because var4 is of type Four, and when calling method1(), it will first go to the calling class and will search the method there. Only if the method was not found there, it will try to see if on of the the ancestors has it. Take a look at my explanation above.

Ok.. I just reread this entire thread before asking again... but...

I understand the super class... however, I dont see how method 1 is called with Var4.Method2 ......

And when casting... ((Two)var1).method3(); does this mean the original One var1 = new Two(); ..... does this mean... it's now Two var 1 = new Two() ?

Every time a method is called, the program first search it in the calling class, if it can't be found it searches in the parent's class, and if it can't find it there then in the parent's parent, and so on.
Now, we have

Three var4 = new Four();
Var4.Method2();

The program first searches class Four for method2(), but it is not there, so it moves one up to class Three, where it finds it. Inside method2() of class Three we are invoking method1(). Again, since var4 was the original caller, we are searching first in class Four - and this time we find method1() in class Four, so we are using it.
I know that this is a little bit confusing, but basically when you have the code:

ParentClass var = new ChildClass()

It means that you can only invoke methods and members that exist in the ParentClass, but when those methods are invoked, due to polymorphism, we will first seach in ChildClass, and only if they were not found in the ChildClass we will go to the ParentClass.

I wrote the problem down wrong on my paper. I didnt realize super.m1 was called in method 2. Thanks very much.

I am still confused about the casting a little though.. test in 20 mins lol

Hope that what I have told you will help you, and good luck! Please mark the thread as solved if you don't need any more help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.