Sei sulla pagina 1di 25
UNIT2 [81 Object-oriented eaturesin classes Summary of Unit 2 Q 0 ‘This has been a rather lengthy unit chat contains plenty of sample class definitions. Each sample class definition demonstrates 2 single feature, You should pay atcention to the differences among them, so that you can understand the effects of the changes, We first discussed ewo important programming concepts — hhey are implemented by marking attributes private and providing corresponding pobi== information hiding and encapsulation. getter/seter methods, The idea of information hiding suggests hac object attributes should be keps out of reach from the users of the object, so that it is not possible to assign invalid values to the attributes. The concept of encapsulation recommends that internal is not ‘operations are transparent to the users of the clas, so that necessary for the users ofthe elass to know its internal operations. ‘Then, if ics internal operations need modifications, the changes are localised and no other classes need imodifications, ‘There are a few steps in inivialising an object during its creation process, In previous units, we mentioned there were implicit and explicit initialisutions of the object attributes. In this unit, we further discussed how a special method ~ the constructor — is called during the creation proces, [ts format is similar to a normal method except the method name is identical to the class name and ic defines no return type. Java features inheritance, which enablesa class to be derived based fon an existing class, In other words, the definition of the derived class (a subclass) extends the definition of the existing class (the superclass) with an extends clause in the class definition. Such a feature enables a subclass to reuse the definition of the superclass and hence minimises duplicated code by the ‘copy-and-paste’ approach. Furthermore, there isan inheritance relationship between the subelass and the superclass in which the subelass inherits all actributes and methods from the superclass. The inheritance relationship between the subclass and the superclass discloses that a subclass “isa? superclass. As a resule, a subclass object can be considered a superclass object. At the same time, we mentioned the ‘has a’ relationship indicates the possession relationship benween vo classes, tis possible to have two methods of the same name. If a class definition has two different methods with the same name but different parameter lists, it is considered to be overloading, The 8 “Tanguage, WAWASAN OPEN UNIVERSITY NEC 102 Computing cone to be executed is determined by the parameter value lst supplied to the method, Ifa subclass defines a method with the same name ‘and parameter list as a method in the superclass, ic is considered to be overriding, and the method defined in the subclass overrides the one defined in the superclass. If a subclass object receives a message and the message corresponds to a method defined in the superclass and the subclass does not define such a method, the ‘method defined in the superclass is executed. Otherwise, the method defined by the subclass overrides a method defined in the superclass, ‘and the subelass overriding method will be executed instead. Polymorphism is the ability for objects that are subclasses of the same parent cass to behave differently on receiving the same message tha is defined by the common parent clas. Inthe Java programming is implemented as a variable of a superclass type, say class r, that can refer co an object oF any subclass of dass 7. Then, ‘fa message js sent fo the object via the variable of type class 7, the behaviour (or the method to be executed) is determined by che actual class of the object (that is, a subclass of class ® or class 7 itself), not by the type of the variable. = Ss ‘The dree key features of an object-oriented programming language — encapsulation, inheritance and polymorphism — are discussed in the unit with their contributions in promoting software writing and itiproving software productivity and maintainability, It is much ‘more impostant for you to realise their significance and the ways to use them in objectoriented programming, UNIT2 [83 Objece-orented features classes Unit Practice Exercises 1. Java uses “garbage collection’ for memory management. Explain what is meant here by garbage collection. What isthe alternative to garbage collection? 2. For this problem, you should write a very simple but complete class. The class represents a counter that counts 0, 1, 2, 3, 4y... The name of the class should be Counter. It has one private instance variable representing the value of the counter. Ie has two instance methods: increment () adds one to the counter value, and getVaiue (} returns the current counter value. Write a complete definition for the class, counter. 3. This problem uses the Counter class from Question 2. The following program segment is meant to simulate tossing a coin 100 times. It should use two Counter objects, headCount and tailCount, to count the number of heads and the number of tails. Fill in the blanks so thar ic will do so Counter headcount, tailcount, tailcount = new Counter (}; headcount ~ neu Counter (1; for ( int flip = 0; flip < 100; flipt+) ( J/ There's @ 50/50 chance that this is true. 4 (ath. random() < 0.5) + fF Count a “head”. else + FF Count a “taal” , system.out .printin(™there were * + + heads." system.out.printin ("There w ne + ails.) 4. Consider the following classes: public clase Animalousece> private B animals public void setaAnimal(E x) animal = x7 , public £ getanimal() | return animal; , 1 public class Animal{) public clase Cat extends Animal () public class Dag extends Animal (1

Potrebbero piacerti anche

  • WUC103 Unit2-2
    WUC103 Unit2-2
    Documento10 pagine
    WUC103 Unit2-2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-3n4
    TCC102 Unit5-3n4
    Documento14 pagine
    TCC102 Unit5-3n4
    api-26781128
    Nessuna valutazione finora
  • WUC103 Unit2-4
    WUC103 Unit2-4
    Documento10 pagine
    WUC103 Unit2-4
    api-26781128
    Nessuna valutazione finora
  • WUC103 Unit2-3
    WUC103 Unit2-3
    Documento16 pagine
    WUC103 Unit2-3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-1
    TCC102 Unit5-1
    Documento22 pagine
    TCC102 Unit5-1
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-2
    WLA105 Unit2-2
    Documento6 pagine
    WLA105 Unit2-2
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit5-3
    WLA105 Unit5-3
    Documento18 pagine
    WLA105 Unit5-3
    api-26781128
    Nessuna valutazione finora
  • WUC103 Unit2-1
    WUC103 Unit2-1
    Documento24 pagine
    WUC103 Unit2-1
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit5-1-2
    WLA105 Unit5-1-2
    Documento22 pagine
    WLA105 Unit5-1-2
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-1
    WLA105 Unit2-1
    Documento20 pagine
    WLA105 Unit2-1
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit4-3
    WLA105 Unit4-3
    Documento18 pagine
    WLA105 Unit4-3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-5
    TCC102 Unit4-5
    Documento22 pagine
    TCC102 Unit4-5
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit4-1-2
    WLA105 Unit4-1-2
    Documento24 pagine
    WLA105 Unit4-1-2
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-3
    WLA105 Unit2-3
    Documento10 pagine
    WLA105 Unit2-3
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-4
    WLA105 Unit2-4
    Documento24 pagine
    WLA105 Unit2-4
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-5
    TCC102 Unit5-5
    Documento12 pagine
    TCC102 Unit5-5
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-1
    TCC102 Unit4-1
    Documento14 pagine
    TCC102 Unit4-1
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-4
    TCC102 Unit4-4
    Documento14 pagine
    TCC102 Unit4-4
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-2
    TCC102 Unit4-2
    Documento26 pagine
    TCC102 Unit4-2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 Summary
    TCC102 Unit3 Summary
    Documento16 pagine
    TCC102 Unit3 Summary
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-2
    TCC102 Unit5-2
    Documento20 pagine
    TCC102 Unit5-2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-3
    TCC102 Unit4-3
    Documento12 pagine
    TCC102 Unit4-3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 4
    TCC102 Unit3 4
    Documento14 pagine
    TCC102 Unit3 4
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 3
    TCC102 Unit3 3
    Documento14 pagine
    TCC102 Unit3 3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 2
    TCC102 Unit3 2
    Documento20 pagine
    TCC102 Unit3 2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 1
    TCC102 Unit3 1
    Documento14 pagine
    TCC102 Unit3 1
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 5
    TCC102 Unit2 5
    Documento13 pagine
    TCC102 Unit2 5
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 3
    TCC102 Unit2 3
    Documento12 pagine
    TCC102 Unit2 3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 4
    TCC102 Unit2 4
    Documento24 pagine
    TCC102 Unit2 4
    api-26781128
    Nessuna valutazione finora