• No se han encontrado resultados

5. METODOLOGIA

6.10 TIEMPO DE MORTALIDAD NEONATAL Y FACTORES CLINICOS DE LOS

The super keyword is used to invoke a parent class constructor. The compiler adds super() with empty parentheses if a constructor does not explicitly use the super keyword, causing the parent’s no-argument constructor to be invoked.

What happens, however, if the parent class does not have a no-argument constructor? Calling super() with empty parentheses does not compile, and the child class constructors need to explicitly invoke a parent constructor, passing in the appropriate arguments to the parent class constructors.

For example, suppose we modify the Television class as follows so that it no longer has a no-argument constructor.

public class Television {

public int channel, volume; public Television(int c) {

this(c,5);

System.out.println(“Inside Television(int)”); }

public Television(int c, int v) {

System.out.println(“Inside Television(int, int)”); channel = c;

volume = v; }

}

Figure 6.6 shows what happens when the BigScreenTV class is compiled using this new Television class.

Figure 6.6 The call to super() no longer works, since Television does not have a no-argument constructor.

The BigScreenTV constructors must explicitly invoke one of the construc- tors in the parent class. The following class shows a whole new BigScreenTV class that uses the super() keyword and passes in arguments to a specific Tele- vision constructor.

public class BigScreenTV extends Television {

public String aspectRatio; public short size;

public BigScreenTV(int channel) {

this(“unknown”, (short) 40, channel);

System.out.println(“Inside BigScreenTV(int)”); }

public BigScreenTV(String r, short s, int channel) {

super(channel);

System.out.println(“Inside BigScreenTV(String, short, int)”); aspectRatio = r;

size = s; }

public BigScreenTV(String r, short s, int channel, int volume) {

super(channel, volume);

System.out.println(“Inside BigScreenTV(String, short, int, int)”);

aspectRatio = r; size = s; }

The following ParentDemo program invokes two BigScreenTV objects using the new Television and BigScreenTV classes. Try to determine what the output will be. Compare your answer with the actual output shown in Figure 6.7.

public class ParentDemo {

public static void main(String [] args) {

System.out.println(“*** big screen #1 ***”); int channel = 4; new BigScreenTV(channel); short size = 53; channel = 3; String ratio = “16:9”;

System.out.println(“\n*** big screen #2 ***”); new BigScreenTV(ratio, size, channel);

ratio = “5:4”; size = 42; channel = 4; int volume = 7;

System.out.println(“\n*** big screen #3 ***”); new BigScreenTV(ratio, size, channel, volume); }

}

Understanding Inheritance 167

The Default Constructor

In Chapter 5, “Methods,” I discussed how the compiler generates a default constructor for any class that does not explicitly declare a constructor. I mentioned that the default con- structor does not contain any statements, but that is only partially true. Because the default constructor is a constructor that does not invoke this() or super(), the compiler adds a call to super() for you.

For example, suppose the BigScreenTV class looked similar to: public class BigScreenTV extends Television {

public String aspectRatio; public short size;

}

The compiler would then generate and add the following constructor to this BigScreenTV class:

public BigScreenTV() {

super(); }

This BigScreenTV class will not compile for the Television class that did not include a no- argument constructor. The BigScreenTV default constructor does not work in this situation. By writing a class without a no-argument constructor, you are forcing all child classes to explicitly add a constructor that contains a call to super(). For the most part, this is not a major concern; however, this is another instance of why you should have a specific reason to write a class that does not contain a no-argument constructor.

Figure 6.7 The output of the ParentDemo program.

Lab 6.1: Implementing Inheritance

The purpose of this lab is to become familiar with implementing inheri- tance in Java. You will write a class named Polygon that is subclassed by a Triangle class.

1. Write a class called Polygon that has two fields: an int for the num- ber of sides and a double for the area. Add a method called get- NumberOfSides() that prints out and returns the number of sides. 2. Override the toString() method in Polygon to return a nice string

representation of your Polygon class.

3. Add a constructor to Polygon that takes in an int to represent the number of sides and prints out the message “Inside Polygon constructor.”

4. Save and compile the Polygon class.

5. Write a class called Triangle that extends Polygon. Add two int fields: one for the base and one for the height. (Triangles have a base and a height.)

6. Add a constructor to Triangle that takes in two int’s for the base and height. The constructor needs to use super() to invoke the construc- tor in Polygon, passing in 3 for the number of sides. Print out the message “Inside Triangle constructor.”

7. Add a toString() method to Triangle that prints out the triangle’s base and height.

8. Add a getArea() method to Triangle that computes and returns the area. The formula for the area of a triangle is:

area = 1/2 (base * height)

9. Save and compile the Triangle class.

10. Write a program that instantiates at least one Polygon object and one Triangle object. Invoke the various methods to ensure that everything is working.

When you instantiate a Triangle object, the output should display “Inside Polygon constructor” before displaying “Inside Triangle constructor.”

Lab 6.2: Overriding Methods

This lab is a continuation of Lab 6.1 and demonstrates overriding methods.

1. Write a class called RightTriangle that extends your Triangle class from Lab 6.1.

2. Add a field of type double called hypotenuse to represent the longest side of a right triangle.

3. Add a constructor that takes in two int’s to represent the base and height. Pass these two values up to the Triangle constructor and then use these two values in the constructor to compute the hypotenuse field. The formula is:

hypotenuse = sqrt(base*base + height*height)

4. Use the Math.sqrt() function to compute the square root. Math.sqrt() takes in a double and returns a double. Also, print out a message stating “Inside RightTriangle constructor.”

5. Add the toString() method to RightTriangle. Use super to invoke toString() in the parent and concatenate the result with the hypotenuse.

6. Save and compile the RightTriangle class.

7. Write a program that instantiates a RightTriangle object and invokes the toString(), getArea(), and getNumberOfSides() methods. Run your program, and verify that everything is working correctly. When you instantiate a RightTriangle object, the output of the con- structors should be in the following order: “Inside Polygon constructor,” “Inside Triangle constructor,” and “Inside RightTriangle constructor.”

Documento similar