inheritance
play

Inheritance Many objects have a hierarchical Inheritance - PDF document

Inheritance Many objects have a hierarchical Inheritance relationship Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software design to take advantage of relationships, supporting reuse


  1. Inheritance • Many objects have a hierarchical Inheritance relationship – Examples: zoo, car/vehicle, card game, airline reservation system • Inheritance allows software design to take advantage of relationships, supporting reuse • Supports the IS-A relationship – what’s the HAS-A relationship? Terminology Syntax • Base class/Parent class/Superclass public class Student extends Person {…} – defines generic functionality public class Derived extends Base{…} • Derived class/Child class/Subclass – extends or specializes base class public class Person { public class Student extends Person { … … void print(); void print(); } } Subclasses Method Invocation • Person – print, getName • Inherit members of parent • Student – print, changeMajor • May implement new members • May override members of parent Person p = new Person("Bob"); Student s = new Student("Alice", "CS"); • Person • Student s.getName(); p.print(); – name – major s.print(); – Person(String) – Student(String, String) p.changeMajor("Econ"); – print() – print() s.changeMajor("Econ"); – getName() – changeMajor(String) 1

  2. Method Invocation Subclasses • Person – print, getName • Person • Student • Student – print, changeMajor – name – major – Person(String) – Student(String, String) Person p = new Person("Bob"); Student s = new Student("Alice", "CS"); – print() – print() s.getName(); //Person getName – getName() – changeMajor(String) p.print(); //Person print s.print(); //Student print p.changeMajor("Econ"); //ERROR s.changeMajor("Econ"); //Student changeMajor Protected Partial Overriding • private members of parent not accessible to //in Person class child class void print() {…} • protected members accessible only to derived classes //in Student class • examples void print() { super.print(); public class Person { //will be accessible in Student // super.methodName(); protected String name; … } } More on super Shadowing Variables public Person(String name) { • Variable (same name) declared in base this.name = name; class and derived class } • Generally, a bad idea public Student(String name, String major) { • Example: Student declares a String super(name); variable name // super(params); this.major = major; } 2

  3. final abstract • Classes and methods can be defined with • abstract classes cannot be instantiated final modifier • Declare abstract class using abstract – final public class Student … keyword – final public void print()… – public abstract class Person … • final classes cannot be extended • Method can also be abstract • final methods cannot be overridden – public abstract void print() • A class with an abstract method must be declared abstract Polymorphism Example • Many forms • A variable is polymorphic if it can refer to Student Staff Faculty different types of objects at different times persondb … Person p = new Person(“Bob”); Person[] persondb = new Person[10]; persondb[0] = new Student(“Sally”, “History”); p = new Student(“Sally”, “History”); persondb[1] = new Staff(…); … persondb[9] = new Faculty(…); Dynamic Binding Dynamic Binding • Determine which method is called based on Person p = new Person(…); object contents Student s = new Student(…); p.print(); //calls Person print() class Person { p = s; //OKAY void print(); p.print(); //calls Student print() } class Student extends Person { p.changeMajor(); //ERROR void print(); } 3

  4. Casting Example Person p; Class1 Class2 extends Class1 p = new Student(…); 1. f1 3. f2 Student s = (Student)p; 2. f2 4. f3 if(p instanceof Student) s = (Student)p; Class1 c1 = new Class2(); Class2 c2 = new Class2(); • If cast is not successful, runtime error c1.f3(); ClassCastException c1.f2(); • instanceof operator used to determine c2 = (Class2)c1; type c2.f3(); Object base class • All classes derive from Object • Defines several methods that can be overridden – String toString() – boolean equals(Object) – Object clone() 4

Recommend


More recommend