30846_sample%5B2%5D - CSE 1030 3.0 Introduction to Computer...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 1030 3.0 Introduction to Computer Science II Sample test 1 (6 marks) A constructor of a subclass usually starts with a special statement that communicates with its parent class. (a) What is the general syntax of this statement? super(<zero or more arguments>); (b) What is its purpose; that is, why do we use it? It allows the subclass to select the constructor to be used for instantiating the superclass and to pass parameters to it. In other words, it allows the subclass to initialize the state of its parent. (c) Can this statement appear anywhere in the subclass or only in special locations? In the latter case, specify these locations. No, it must be the very first statement of the body of a constructor. (d) What happens if we do not use this statement at all in the subclass? If absent, the compiler automatically inserts super(), that is, one that selects the default constructor of the superclass. 2 (6 marks) Consider the class A shown below. public class A { private int x; private double y; public A(int x, double y) { this.setX(x); this.setY(y); } public void setX(int x) 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
{ this.x = x; } public void setY(double y) { this.y = y; } } We want to create the following subclass. public class B extends A { private String s; public B(String s, int x, double y) { } } Write the body of the constructor. super(x, y); this.s = s; 3 (8 marks) Consider the following UML class diagram in which the Animal class is abstract. Animal Dog 6 Cat 6 (a) Why is the class Animal abstract? Because one should not be able to construct Animal objects (one cannot create instances of an abstract class). 2
Background image of page 2
(b) The class Animal contains a constructor. Explain why is there a constructor even though the class is abstract. To initialize the attributes common to Dog and Cat (these attributes are declared in
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/13/2011 for the course CSE/MATH 1030 taught by Professor Mixprofs during the Winter '09 term at York University.

Page1 / 7

30846_sample%5B2%5D - CSE 1030 3.0 Introduction to Computer...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online