1 Introduction to Inheritance What is Inheritance Superclass

  • Slides: 13
Download presentation
 1

1

Introduction to Inheritance Ø What is Inheritance? Ø Superclass vs. Subclass Ø Why Inheritance?

Introduction to Inheritance Ø What is Inheritance? Ø Superclass vs. Subclass Ø Why Inheritance? Ø Subclass Syntax Ø Final Classes Ø Class Hierarchy Ø Subclass Constructors Ø Example Ø Exercises 2

What is Inheritance? Ø Inheritance is a mechanism for enhancing existing, working classes. Ø

What is Inheritance? Ø Inheritance is a mechanism for enhancing existing, working classes. Ø A new class can inherit from a more general existing class. Ø For Class Child: Ø Attributes: Ø Inherited: a , b Ø not inherited: c Ø Methods: Ø Inherited: op 1( ) , op 2( ) Ø not inherited: op 3( ) Parent a b op 1( ) op 2( ) Child c op 3( ) Ø Note: Constructors and private members of a class are not inherited by its subclasses. Ø Java supports single inheritance: A subclass has one parent only. Ø In multiple inheritance a subclass can have more than one parent. 3

Superclass vs. Subclass Ø Superclass: Ø A general class from which a more specialized

Superclass vs. Subclass Ø Superclass: Ø A general class from which a more specialized class (a subclass) inherits. Ø Subclass: Ø A class that inherits variables and methods from a superclass but adds instance variables, adds methods, or redefines inherited methods. Course Student Graduate. Student Short. Course On. Line. Course defend. Thesis( ) produce. Certificate( ) deliver. Lecture( ) 4

is. A relationship Ø A subclass-superclass pair defines “is. A” relationship: Ø A graduate

is. A relationship Ø A subclass-superclass pair defines “is. A” relationship: Ø A graduate student is a student. Ø A short course is a course. Ø An online course is a course. Ø A graduate student is an object. Course Student Graduate. Student Short. Course On. Line. Course defend. Thesis( ) produce. Certificate( ) deliver. Lecture( ) 5

Why Inheritance? Ø Through inheritance we gain software reuse. Ø Helps in writing structured

Why Inheritance? Ø Through inheritance we gain software reuse. Ø Helps in writing structured programs. Ø It is more natural and closer to real world. 6

Subclass Syntax class Subclass. Name extends Superclass. Name { variables methods } Ø Example:

Subclass Syntax class Subclass. Name extends Superclass. Name { variables methods } Ø Example: class Graduate. Student extends Student { String thesis. Title; . . . defend. Thesis(){. . . } 7

Final Classes Ø A class that is declared as final cannot be extended or

Final Classes Ø A class that is declared as final cannot be extended or subclassed. final class. Name { variables methods } Ø Examples of final classes are: Ø java. lang. System. Ø The primitive type wrapper classes: Byte, Character, Short, Integer, Long, Float, and Double. 8

Class Hierarchy Ø In Java, every class that does not extend another class is

Class Hierarchy Ø In Java, every class that does not extend another class is a subclass of the Object class. that is defined in the java. lang package. Ø Thus, classes in Java form a hierarchy, with the Object class as the root. Ø Example of a class hierarchy: 9

Subclass Constructors Ø To initialize instance variables of a superclass, a subclass constructor invokes

Subclass Constructors Ø To initialize instance variables of a superclass, a subclass constructor invokes a constructor of its superclass by a call of the form: super(paraneters); Ø This statement must be the first statement within the constructor. Ø Example: public Graduate. Student(int id, String name, double gpa, String thesis. Title){ super(id, name, gpa); this. thesis. Title = thesis. Title; } Ø If the first statement in a constructor does not explicitly invoke another constructor with this or super; Java implicitly inserts the call: super( ); . If the superclass does no have a no-argument constructor, this implicit invocation causes compilation error. Ø Constructor calls are chained; any time an object is created, a sequence of constructors is invoked; from subclass to superclass on up to the Object class at the root of the class hierarchy. 10

Example class Vehicle{ private String vehicle. Id. Number ; private String vehicle. Chassis. Number

Example class Vehicle{ private String vehicle. Id. Number ; private String vehicle. Chassis. Number ; private String model ; public Vehicle(String vin, String vcn, String model){ vehicle. Id. Number = vin; vehicle. Chassis. Number = vcn; this. model = model; } public String to. String( ){ return "Vehicle ID = " + vehicle. Id. Number + "n. Vehicle Chassis Number = " + vehicle. Chassis. Number + "n. Vehicle Model = " + model; } public boolean equals(Vehicle vehicle){ return this. vehicle. Chassis. Number == vehicle. Chassis. Number; } } class Bus extends Vehicle{ private int number. Of. Passengers ; public Bus(int num. Passengers, String vin, String vcn, String model){ super(vin, vcn, model) ; number. Of. Passengers = num. Passengers ; } public int get. Number. Of. Passengers( ){ return number. Of. Passengers ; } } 11

Example (cont’d) class Truck extends Vehicle{ private double cargo. Weight. Limit ; public Truck(double

Example (cont’d) class Truck extends Vehicle{ private double cargo. Weight. Limit ; public Truck(double weight. Limit, String vin, String vcn, String model){ super(vin, vcn, model) ; cargo. Weight. Limit = weight. Limit ; } public double get. Cargo. Weight. Limit( ){ return cargo. Weight. Limit ; } } public class Vehicle. Test{ public static void main(String[] args){ Vehicle vehicle = new Vehicle ("QMY 489", "MX-0054322 -KJ", "BMW 500"); Bus bus 1 = new Bus(30, "TMK 321", "AF-987654 -WR", "MERCEDEZ BENZ"); Bus bus 2 = new Bus(30, "2348976", "AF-987654 -WR", "MERCEDEZ BENZ"); Truck truck = new Truck(10. 0, "DBS 750", "RZ-70002345 -PN", "ISUZU"); System. out. println(vehicle) ; System. out. println(bus 1) ; System. out. println(truck) ; if(bus 1. equals(bus 2)) System. out. println("Bus 1 and Bus 2 are the same") ; else System. out. println("Bus 1 and Bus 2 are not the same") ; } } 12

Exercises Question 1: What inheritance relationships would you establish among the following classes: Student,

Exercises Question 1: What inheritance relationships would you establish among the following classes: Student, Professor, Teaching Assistant, Employee, Secretary, Department. Chairman, Janitor, Person, Course, Seminar, Lecture, Computer. Lab Question 2: In the following pairs of classes, identify the superclass and the subclass: Manager -Employee, Polygon -Triangle, Person - Student, Vehicle - Car, Computer-Laptop, Orange - Fruit. 13