Documente online.
Zona de administrare documente. Fisierele tale
Am uitat parola x Creaza cont nou
 HomeExploreaza
upload
Upload




Reusing Classes

computers


3.3 Reusing Classes

3.3.1 Questions

1. Which are the possible ways to reuse a class?

2. Consider having class A, class B extends A and class C extends B. Now create an object of class C. Which is the order the constructors are called?

3. How can you call the constructor from the base class in a derived class constructor? When c 616v213g an you do that?



4. What would you use to build up an electronic document with text and images - composition or inheritance? But to model a Word document, an Excel spreadsheet and an HTML page?

5. How can you make a member private for normal clients and still visible for derived classes?

6. Can you change the value of a final object argument? For example, if a method takes a final List argument, can you call clear on it?

7. Are the methods of a final class also final?

3.3.2 Problems

1. Write a test program to demonstrate the answer to question 6.

2. Take the file Cartoon.java and comment out the constructor for the Cartoon class. Explain what happens.

3. Prove that default constructors are created for you by the compiler.

4. Create a class called Amphibian. From this, inherit a class called Frog. Put appropriate methods in the base class. In main( ), create a Frog and upcast it to Amphibian, and demonstrate that all the methods still work.

5. Use inheritance and composition to model a graphical object editor. (Solution: have a Graphic class, add a Point that extends Graphic, add a Circle that extends Graphic and has a Point as center, add a Line that extends Graphic and has two Points as the ends).

3.4 Polymorphism

3.4.1 Questions:

1. What is the polymorphism?

2. What is binding? Classify method binding.

3. Which methods can not be overridden?

4. Which is the result of the following code? Explain the result.

class A

private String getName()

}

class B extends A

}

public static void main(String[] args)

5. Why is not recommended to call methods inside constructors?

6. What is wrong in the following code:

class A

protected printValue()

}

class B

}

Propose solutions for correcting the above code.

3.4.2 Problems:

1. Create an abstract class with no methods. Derive a class and add a method. Create a static method that takes a reference to the base class, downcasts it to the derived class, and calls the method. In main( ), demonstrate that it works. Now put the abstract declaration for the method in the base class, thus eliminating the need for the downcast.

2. Create a base class with two methods. In the first method, call the second method. Inherit a class and override the second method. Create an object of the derived class, upcast it to the base type, and call the first method. Explain what happens.

3. Consider the folowing code:

4. B b = new B();

5. b.print(); // should print "from b".

6. ((A)b).print(); //should print "from a".

7. Implement those two classes in order to obtain the expected outputs.


Document Info


Accesari: 1417
Apreciat: hand-up

Comenteaza documentul:

Nu esti inregistrat
Trebuie sa fii utilizator inregistrat pentru a putea comenta


Creaza cont nou

A fost util?

Daca documentul a fost util si crezi ca merita
sa adaugi un link catre el la tine in site


in pagina web a site-ului tau.




eCoduri.com - coduri postale, contabile, CAEN sau bancare

Politica de confidentialitate | Termenii si conditii de utilizare




Copyright © Contact (SCRIGROUP Int. 2024 )