Classes and Sub-classes - how do you track them?

Barak Yaari

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

Aheinlein

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to model classes that do calculations and store them?

From Dev

how do you implement classes with property subsets?

From Dev

how do you add multiple hover on classes

From Dev

How do you use a string across classes?

From Dev

How do you set generated classes from the same schema but in different generations to eachothers request/response objects without traversing them?

From Dev

How do I document "sub-classes" using sphinx and autodoc?

From Dev

How to keep track of classes that Jackson might deserialize to?

From Dev

How to keep track of css classes in a menu?

From Dev

How do you access the 'request' object (or similar) from custom classes?

From Dev

How do you convert WSDLs to Java classes using Eclipse?

From Dev

How do you add/remove a list of classes from an element?

From Dev

How do you dynamically compile and load external java classes?

From Dev

In Haxe, how do you add Types/Classes to a Module with macros?

From Dev

How do you avoid specifying template parameter for classes?

From Dev

How do you access variables from different classes in Java?

From Dev

how do you blit text to screen using classes in pygame?

From Dev

How do you create and use variables of custom classes for a webapp?

From Dev

How Do You Pass PropertyChanged Events Between Classes?

From Dev

How do you copy feature classes into a new geodatabase in ArcGIS/Python?

From Dev

How do you handle multiple application classes in a xamarin android project

From Dev

Do sub classes need to implement Interfaces?

From Dev

Do child classes inherit parent constants and if so how do I access them?

From Dev

how to use abstract classes, and implementing them

From Dev

Swift: When do you need to import classes?

From Dev

How do you mock classes that are used in a service that you're trying to unit test using JUnit + Mockito

From Dev

How do I get a trait to know about the members of it's sub classes in Scala?

From Java

Can you monkey-patch built in classes and if not, how do I overload an operator to define addition for two different classes?

From Dev

Create a vector of sub classes?

From Dev

Create a vector of sub classes?

Related Related

  1. 1

    How to model classes that do calculations and store them?

  2. 2

    how do you implement classes with property subsets?

  3. 3

    how do you add multiple hover on classes

  4. 4

    How do you use a string across classes?

  5. 5

    How do you set generated classes from the same schema but in different generations to eachothers request/response objects without traversing them?

  6. 6

    How do I document "sub-classes" using sphinx and autodoc?

  7. 7

    How to keep track of classes that Jackson might deserialize to?

  8. 8

    How to keep track of css classes in a menu?

  9. 9

    How do you access the 'request' object (or similar) from custom classes?

  10. 10

    How do you convert WSDLs to Java classes using Eclipse?

  11. 11

    How do you add/remove a list of classes from an element?

  12. 12

    How do you dynamically compile and load external java classes?

  13. 13

    In Haxe, how do you add Types/Classes to a Module with macros?

  14. 14

    How do you avoid specifying template parameter for classes?

  15. 15

    How do you access variables from different classes in Java?

  16. 16

    how do you blit text to screen using classes in pygame?

  17. 17

    How do you create and use variables of custom classes for a webapp?

  18. 18

    How Do You Pass PropertyChanged Events Between Classes?

  19. 19

    How do you copy feature classes into a new geodatabase in ArcGIS/Python?

  20. 20

    How do you handle multiple application classes in a xamarin android project

  21. 21

    Do sub classes need to implement Interfaces?

  22. 22

    Do child classes inherit parent constants and if so how do I access them?

  23. 23

    how to use abstract classes, and implementing them

  24. 24

    Swift: When do you need to import classes?

  25. 25

    How do you mock classes that are used in a service that you're trying to unit test using JUnit + Mockito

  26. 26

    How do I get a trait to know about the members of it's sub classes in Scala?

  27. 27

    Can you monkey-patch built in classes and if not, how do I overload an operator to define addition for two different classes?

  28. 28

    Create a vector of sub classes?

  29. 29

    Create a vector of sub classes?

HotTag

Archive