Chapter 10 — Encapsulation, Inheritance, Polymorphism
Part 1A — Learn Dart · Chapter 10 of 62 · 7 h
Chapter 10 — Encapsulation, Inheritance, Polymorphism
Level: intermediate Estimated time: 7 h Prerequisites: Chapter 09 — Constructors and Modeling What you will be able to do by the end: protect a class's data with private members and validated getters and setters, build a class hierarchy with
extendsandsuper, override methods with@override, and write polymorphic code that handlesPlayer,Enemy, andBossthe same way.
10.0 — Chapter Objectives
By the end of this chapter, you will be able to:
- name the three OOP pillars covered here;
- explain what encapsulation is in your own words;
- show concretely the danger of a public property that can be modified any which way;
- declare a private member with the
_prefix; - explain that the scope of
_is the library (the file), not the class; - write a getter that exposes a read-only value;
- write a setter that validates a value before accepting it;
- write private helper methods for internal use;
- build a fully encapsulated
Playerclass, with lives always bounded between 0 and a maximum; - explain what inheritance is and what it is for;
- use
extendsto create a subclass; - say precisely what is inherited and what is not;
- call the parent constructor with
super; - call a parent method with
super.methode(); - override an inherited method;
- use the
@overrideannotation correctly; - explain polymorphism using an example;
- iterate over a
List<Character>containingPlayerandEnemyobjects; - use
isandasto test and cast a type; - build the
Character → Player / Enemy / Bosshierarchy; - recognize the situations where inheritance is a poor choice.
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion