Classes and subclasses - how do you track them?

My fellow students (1st year CS) and I are struggling with classes and subclasses. We have a test in which one of the questions checks the understanding of classes, subclasses, legal and illegal casting between them, etc. The questions, as a rule, are the same: you are given 2-3 classes with the name A, B, C, some of them expand others, and then different constructors and 10-15 very confusing lines of code, on each of them we need to say whether This is to one of the following: 1. A runtime error. 2. Compilation error. 3. It will start (which will be printed).

A test is just paper. The question is, do you have a method for tracking various classes and variables? is there a site that teaches these subjects?

This is a question from a previous test: Which of the following lines of code will cause a compilation error, a runtime error, and what will be printed: (I added a 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

Grade 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); 
    } 
} 

Grade 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); 
    } 
} 

Grade 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 this is a long time, but I wanted you to understand the type of questions ... I am not looking for answers, I try to help me understand the concepts and ways to think it through. The main problems we are struggling with are when you extend one class and then call a method inside it, which calls another method in a subclass, and so on ... Keeping track of all that is complicated. Thanks a lot, Barak

+3
source share
4 answers

A compilation error usually occurs when your syntax is incorrect or you did not instantiate the object correctly.

println (Hello World) - , Hello World Hello World, . , String Hello = "Hello"; = ""; -. . , . , println (Hello + World). , "HelloWorld", println().

a = b , - . , "", , B A, : "B - A". , (A) (B), , " - "

a.equals(1); , a1 . , ""

System.out.println(a.equals(2)); false, equals , a2 "" A. , , x y . , x y , a2 - .

System.out.println(b.equals()); , "c A", x y b c. 1 2, .

B aa = (B) a; a , . , "B - A", "A - B", , "Shape is a Circle", , . C bbb = (C) bb; , bb A.

a.m1();// m2. ( ) , m1(), - m2(). "2". , a.m1(), , a.m2().

b.m1();// 3. B m2() m4(), m1() → m2() → m3() → m4(), m4() y . B (x, y1, y2) x = x; y = y1; ( super (x, y1)), y y2.

c.m3();// 2. . , m3() → m4(). C , , A. A m4() A B (1,2,3). B y2 (3), A , B A x , , .

a.m5();// false , "System.out.println(a.equals(a2)); . c null, C C(), C (0,0). null C C, .

, , , , , , . ( ) . , .

- x c.m3()

+1

Aheinlein

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

"instanceof" "null", a2 A. , "false" , "a2" null, , x y.

+1

, , , . , , , , .

№ 1

. . System.out.println, , . . , . , . , . .

№ 2

. ( ):

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

, , . , , , , , , . .

№ 1

, 1 2, ​​ , . , , , .

0

Use a good IDE (Eclipse or Netbeans) first, then try different hierarchies. I offer you O'reilly Head First Java, a great book for beginners. There is also a cookbook for practice.

Only you can improve yourself. Start reading now.

0
source

All Articles