Question

Me and my fellow students (1st year CS) are struggling with the issue classes and sub-classes. We have a test, in which one of the questions checks the understanding of Classes, sub-classes, Legal and illegal casting between them etc. The questions are usually the same: You are given 2-3 classes named A,B,C, some of them extend others, and then different constructors and a 10-15 very confusing lines of code, on each of them we need to tell if it will cause one of the following: 1. Run-time error. 2. Compilation error. 3. Will run (what will be printed out).

The test is paper only. The question is, do you have a method of tracking the different classes and variables? is there a site that teaches these subjects?

This is the question from the previous test: Which of the following lines of code will cause Compilation error, runtime error, and what will be printed: (I added the solution)

A a = new A(1,2); 
B b = new B(1,2,3); 
C c = new C(1,2); 
A bb = (A) b; 
A a1; 
A a2 = null; 
System.out.println("Hello world"); //"Hello World
System.out.println(Hello world); //Compilation error
System.out.println(1/0); //Runtime Error
a = b; //Will run with no errors
a.equals(a1); //compilation error
System.out.println(a.equals(a2));//will print false. 
System.out.println(b.equals(c)); //will print true.
B aa = (B) a; //runtime error.
C bbb = (C) bb; //runtime error
C ccc = (C) b; //compilation error
a.m1(); //will print m2
b.m1(); //will print 3
c.m3(); //will print 2.
a.m5(); //will print false

Class A:

 public class A { 
    private int x; 
    public int y; 
    private C c = null; 
    public A(int x,int y){ 
        this.x = x; 
        this.y = y; 
    } 
    public void m1() { m2();} 
    public void m2() { 
        System.out.println("m2"); 
    } 
    public void m3(){ m4(); } 
    public void m4() { 
        A tmp = new B(1,2,3); 
        System.out.println(tmp.y); 
    } 
    public void m5() { 
        System.out.println((new C().equals(c))); 
    } 
    public boolean equals(Object other) { 
        if(!(other instanceof A)) return false; 
        return (((A)other).x==x & 
                ((A)other).y==y); 
    } 
} 

Class B:

public class B extends A { 
    public int y; 

    public B(int x, int y1, int y2){ 
        super(x,y1); 
        this.y = y2; 
    } 

    public void m2(){ 
        m3(); 
    } 

    public void m4() { 
        B temp = new B(1,2,3); 
        System.out.println(temp.y); 
    } 
} 

Class C:

public class C extends A { 
    public int y; 
    public C(int x, int y){ 
        super(x,y); 
        this.y = y; 
    } 

    public C(){ 
        this(0,0); 
    } 
} 

I know it is long, but I wanted you to understand the type of the questions... I'm not looking for the answers, try to help me understand the concepts and the ways to think this through. The main issues we are struggling with are with when you extend one class, then call a method within it, that calls a different method in a subclass and so and so.... Tracking all that is difficult. Thank's a lot, Barak

Was it helpful?

Solution

a compilation error usually occurs when your syntax is incorrect, or you have not instantiated an object correctly.

println(Hello World) is a compilation error because Hello and World are trying to refer to the variable Hello and World which have not been instantiated. to solve this part, above it you could say String Hello = "Hello"; String World = "World"; That would solve the instantiate error. But this is also syntactically incorrect. if a function takes multiple parameters, they must be separated by commas. to correct this part, println(Hello + World). This puts the 2 strings together to make a single parameter "HelloWorld" which is used in the println() function.

a = b is ok because both have been instantiated and because of inheritance. think of inheritance as saying "is a" so when B extends A, you can say "B is a A". using another example, a shape(A) and a circle (B) you can say "a circle is a shape"

a.equals(a1); is a compilation error because a1 has not been instantiated. to instantiate an object, you need to use the word "new"

System.out.println(a.equals(a2)); is false because the equals function first checks if a2 "is a" A. it is, so it checks if the x's and y's are the same value. they are not since a's x and y are set but a2 is just a null object.

System.out.println(b.equals(c)); is true because again, "c is a A" so it checks the x and y of b and c. they are 1 and 2, so it is true.

B aa = (B) a; a has been instantiated and it is syntactically correct so it passes compilation. However you can say "B is a A" but not "A is a B" like you cant say a "Shape is a Circle" when it tries to run, it notices that and gives the runtime error. C bbb = (C) bb; is the same way since bb is an A object.

a.m1(); //will print m2. The first (and only) command that the function m1() does is call m2(). this prints "m2". so when you call a.m1() it is the same as calling a.m2() directly.

b.m1(); //will print 3. since B overwrites the m2() and m4() functions, it calls m1() -> m2() -> m3() -> m4() and m4() creates a variable and prints its y value. the constructor for B(x,y1,y2) sets x = x; y = y1; (from the super(x,y1) function) but then overwrites the y with y2.

c.m3(); //will print 2. this is confusing. like the last one, this one calls m3() -> m4(). since C does not overwrite either of them, it uses the ones defined by A. A's m4() creates an A from a new B(1,2,3). The B object's y value gets overridden by the y2(3), but since A's x value is private, the B constructor cant touch A's x value and therefore cannot overwrite it.

a.m5(); //will print false is the same as "System.out.println(a.equals(a2));" above. A's c value is null, and C has a constructor with no arguments C() which calls C(0,0). a null C is not the same as a C object that has been instantiated.

I hope this is at least a good start into what you were looking for, and I hope my explanations make sense. Sometimes it is tough to find the balance between over explaining (having too many words can be confusing) and not enough. Make some comments if you need some more clarification.

Edited - I added a bit about the private variable x in the c.m3() part

OTHER TIPS

Aheinlein

System.out.println(a.equals(a2));

the "instanceof" command does not allow "null", though a2 is created as A. So the "false" would be returnen because "a2" is null, and not because of the comprehension between it's x's and y's.

There are two ways you can learn and then one way you can apply your learning. In all probability there are more ways than I have listed, however that is not a stackoverflow question, but a question for pedagogy.

Learning Way #1

Create multiple classes without using cut and paste. Use different hierarchies. Use System.out.println to determine which method is being called. Write a main method to exercise various scenarios. Attempt to guess what will happen. Then check it using reality. This s basic test driven development, except used in a learning environment. Works very well for practical people.

Learning Way #2

Read up on classes and inheritance. A basic tutorial (and extremely well worth it) is :

http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html

You will find material written on this site by the people who actually wrote the libraries. Once you get past this tutorial, there are advanced books on inheritance, the effect of access, abstract, interface, final on class structure. No one book manages to explicate every one of these concepts.

Apply Learning #1

Once you have approached random problems of your choice using methods 1 and 2, try and write the code in previous tests and guess the results before your program tells them. You will start to develop a technique for tracing through the paths that will hopefully lead you to the correct answer.

First Use the good IDE(Eclipse or Netbeans) then try various hierarchies. I suggest you O'reilly Head First Java which great book for beginners.There are cookbook also for practice.

Only you can improve yourself.Start reading now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top