Chapter 6 Inheritance

49 361 0
Chapter 6 Inheritance

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

1 Inheritance Inheritance Chapter 6 Chapter 6 2 Outline • Creating subclasses • Class hierarchies • The protected modifier • Overriding methods • Polymorphism via inheritance 3  Inheritance is the process in which a new class is derived from an existing one  Inheritance allows a new class to be written that extends an existing class  The existing class is called the parent class, or super class, or base class  The derived class is called the child class or subclass, derived class A B B inherits A Inheritance Inheritance 4 Inheritance Inheritance  The child class inherits characteristics of the parent class  That is, the child class inherits the variables and methods defined by the parent class  The derivation process should establish a specific kind of relationship between two classes: an is-a relationship  For example: • Car is-a Vehicle • Dictionary is-a Book Book Dictionary Vehicle Car 5 Creating subclasses Creating subclasses  In Java, we use the reserved word extends to establish an inheritance relationship class Car extends Vehicle { // class contents } class Dictionary extends Book { // class contents } 6 Outline • Creating subclasses • Class hierarchies • The protected modifier • Overriding methods • Polymorphism via inheritance 7  A child class of one parent can be the parent of another child  Furthermore, multiple classes can be derived from a single parent  Therefore, inheritance relationships often develop into class hierarchies Class hierarchies Class hierarchies 8  Two children of the same parent are called siblings  Common features should be put as high in the hierarchy  A child class inherits from all its ancestor classes Animal Snake Lizard Bird Parrot Reptile Class hierarchies Class hierarchies 10 Example Example  Cylinder inherits Circle Circle Cylinder 11 Example Example Circle - radius : double + Circle() + Circle(r : double) + getRadius() : double + setRadius(r : double) : void + Area() : double Cylinder - length : double + Cylinder() + Cylinder(r : double, l : double) + getLength() : double + setLength(l : double) : void + Volume() : double TestCylinder + main (args : String[]) : void Class diagram for TestCylinder [...]... class returns true if two references are aliases • We can override equals method in any class to define equality in some more appropriate way 16 Outline • Creating subclasses • Class hierarchies • The protected modifier • Overriding methods • Polymorphism via inheritance 28 The protected modifier  Variables and methods declared with private visibility cannot be referenced in a child class  They can... w.getPages()); System.out.println ("Number of definitions: " + w.getDefinitions()); System.out.println ("Definitions per page: " + w.computeRatio()); } } 46 Outline • Creating subclasses • Class hierarchies • The protected modifier • Overriding methods • Polymorphism via inheritance 47 Overriding methods  Overriding method is creating a method in a subclass that has the same signature (name, number, and type of... void { + getDefinitions() : int return definitions / pages; } public void setDefinitions (int numDefinitions) { definitions = numDefinitions; } public int getDefinitions () { return definitions; } } 36 Words.java public class Words { public static void main (String[] args) { Dictionary w = new Dictionary (1500, 52500); System.out.println ("Number of pages: " + w.getPages()); System.out.println ("Number... They can be referenced in the child class if they are declared with public visibility - but public variables violate the principle of encapsulation  There is a third visibility modifier that helps in inheritance situations: protected 29 The protected modifier  The protected modifier allows a child class to reference a variable or method directly in the child class  It provides more encapsulation . 1 Inheritance Inheritance Chapter 6 Chapter 6 2 Outline • Creating subclasses • Class hierarchies • The protected modifier • Overriding methods • Polymorphism via inheritance 3  Inheritance. class is called the child class or subclass, derived class A B B inherits A Inheritance Inheritance 4 Inheritance Inheritance  The child class inherits characteristics of the parent class  That. Polymorphism via inheritance 7  A child class of one parent can be the parent of another child  Furthermore, multiple classes can be derived from a single parent  Therefore, inheritance relationships

Ngày đăng: 13/05/2014, 10:42

Từ khóa liên quan

Mục lục

  • Inheritance

  • Outline

  • Slide 3

  • Slide 4

  • Creating subclasses

  • Slide 6

  • Class hierarchies

  • Slide 8

  • Example

  • Slide 11

  • Circle.java

  • Cylinder.java

  • TestCylinder.java

  • The Object class

  • Slide 16

  • Slide 28

  • The protected modifier

  • Slide 30

  • Example 1

  • Slide 32

Tài liệu cùng người dùng

Tài liệu liên quan