1 / 30

INHERITANCE

INHERITANCE. GALIH WASIS WICAKSONO. INHERITANCE. The Shape class is the superclass of the other four classes . The other four are the subclasses of Shape. The subclasses inherit the methods of the superclass.

satin
Download Presentation

INHERITANCE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INHERITANCE GALIH WASIS WICAKSONO

  2. INHERITANCE • The Shape class is the superclass of the other four classes. • The other four are the subclasses of Shape. • The subclasses inherit the methods of the superclass. • If the Shape class has the functionality, then the subclasses automatically get that same functionality

  3. INHERITANCE • In Java, we say that the subclass extends the superclass. • An inheritance relationship means that the subclass inherits the members of the superclass. • Instance variables are not overridden. They don’t define any special behavior, so a subclass can give an inherited instance variable any value it chooses

  4. EXAMPLE

  5. INSTANCE VARIABLE • picture– the file name representing the JPEG of this animal • food – the type of food this animal eats. Right now, there can be only two values: meat or grass. • hunger – an int representing the hunger level of the animal. It changes depending on when (and how much) the animal eats. • boundaries – values representing the height and width of the ‘space’ (for example, 640 x 480) that the animals will roam around in. • location – the X and Y coordinates for where the animal is in the space.

  6. METHODS • makeNoise() – behavior for when the animal is supposed to make noise. • eat() – behavior for when the animal encounters its preferred food source, meat or grass. • sleep() – behavior for when the animal is considered asleep. • roam() – behavior for when the animal is not eating or sleeping (probably just wandering around waiting to bump into a food source or a boundary).

  7. CONT’D • When you call a method on an object reference, you’re calling the most specific version of the method for that object type. • In other words, the lowest one wins! • so invoking a method on a reference to a Wolf object means the JVM looking first in the Wolf class. If the JVM doesn’t find a version of the method, it starts walking back up the inheritance hierarchy until it finds a match

  8. BUAT HIRARKI CLASSNYA

  9. IS A DAN HAS A • When one class inherits from another, we say that the subclass extends the superclass • Triangle INHERITS a Shape • Triangle IS-A Shape • Tub INHERITS a Bathroom • Tub IS A Bathroom ? • Bathroom HAS A Tub

  10. Wolf IS-A Canine, so Wolf can do anything a Canine can do. And Wolf IS-A Animal, so Wolf can do anything an Animal can do

  11. PILIH YANG BERELASI IS A ?

  12. ACCESS LEVELS

  13. ACCESS MODIFIERS • public Use public for classes, constants (static final variables), and methods that you’re exposing to other code (for example getters and setters) and most constructors.

  14. CONT’D • private Use private for virtually all instance variables, and for methods that you don’t want outside code to call (in other words, methods used by the public methods of your class). But although you might not use the other two (protected and default), you still need to know what they do because you’ll see them in other code.

  15. CONT’D • protected Protected access is almost identical to default access, with one exception: it allows sub-classes to inherit the protected thing, even if those subclasses are outside the package of the super-class they extend. That’s it. That’s all protected buys you—the ability to let your subclasses be outside your superclass package, yet still inherit pieces of the class, including methods and constructors.

  16. GOOD DESIGN OF INHERITANCE • DO use inheritance when one class is a more specific type of a superclass • DO consider inheritance when you have behavior (implemented code) that should be shared among multiple classes of the same general type

  17. CONT’D • DO NOT use inheritance just so that you can reuse code from another class, if the relationship between the superclass and subclass violate either of the above two rules • DO NOT use inheritance if the subclass and superclass do not pass the IS-A test. Always ask yourself if the subclass IS-A more specific type of the superclass.

More Related