1 / 20

Inheritance and Polymorphism in Java

Understand the concepts of inheritance and polymorphism in Java programming. Learn how classes can inherit properties and methods from a superclass, and how different subclasses can implement these inherited methods in their own unique way.

stach
Télécharger la présentation

Inheritance and Polymorphism in Java

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 and Polymorphism Dr. M.M. van Baal Verdugo Hills High

  2. What does Inheritance mean in English?

  3. What about Polymorphism? • Umm…

  4. Before the Java definitions… • Take out a piece of paper and draw an automobile.

  5. My next automobile.(Anybody draw something like this?):

  6. My next automobile.(According to my wife):

  7. They look a little different • Would you agree they both have… • 4 tires • An engine • Windows • Doors • Seats • Can start their engines • Refuel by putting gas in the tank • Therefore they must both be part of the same class: Automobile

  8. Therefore… • Since they are both the same class, they must be able to do the following the same way too • Accelerate • Make turns • Weave through traffic • Attract attention (both of the police and the opposite sex) • Do you agree?

  9. Not quite… • There are a lot of similarities, both are indeed automobiles • But one is a sports car and one is a minivan • Are those different classes?

  10. More like different subtypes • Think about how sports cars were invented • Did they start all over from scratch? • Someone took the automobile and made a more specific type

  11. How does this work in java? • We take the class: public class Automobile • Members (= fields = instance variables): • public String engine • public int doors • public int seats • Methods (= to “do” things): • startEngine() • refuel() • accelerate()

  12. And extend its definition • We will add and replace what we need • public class SportsCar extends Automobile • makeTurns() • weaveThroughTraffic() • attractAttention() • accelerate() • Notice we’re replacing accelerate() that is defined in Automobile. That’s because the SportsCar needs to expel a very loud purr from the engine as it accelerates. • We will keep (= inherit) everything else: • startEngine() • refuel() • We get this for free and don’t have to write the code again

  13. Inheritance in terms of Java • Put it in your own words!

  14. So, Polymorphism? • 1 Command, implemented differently by different classes (here: different cars). • Example pumpGas( ) method: • Sports car (at back of car). • Minivan (on right side of car). • Luxury car (on left side of car). • Pickup truck (behind rear license plate).

  15. An auto is an auto • In other words, whatever kind of Automobile you have, you can pump gas into it. • Java will ask the Automobile (regardless of what kind of car it is) to pumpGas() and all cars fill gas in their own way e.g. side of the car, rear of the car, etc.

  16. Another polymorphic example. • Consider the superclass: public class Animals. • Animals has an eat() method. • There are several classes that inherit from Animals: • public class Bear extends Animals • public class Chicken extends Animals • public class Tarantula extends Animals • public class Cockroach extends Animals

  17. eat() method: • You can call the eat() method in Animals. • However, every animal eats differently => each animal defines the eat() method in Animals differently. • How do they eat(): • Bear? • Chicken? • Tarantula? • Cockroach? • Did you know a cockroach can live for about a week without its head until it finally dies of hunger? Just info: nothing to do with Java!!!!

  18. Inheritance in GridWorld • class Bug extends Actor • Inherits putSelfInGrid() • Replaces act() • Adds canMove() • class BoxBug extends Bug • Inherits canMove() • Replaces act()

  19. Polymorphism in action • For each Actor (whether Actor, Bug, etc) in the grid, act() is called in each step: for (Actor a : actors) { // only act if another actor hasn't removed a if (a.getGrid() == gr) a.act(); }

More Related