140 likes | 400 Views
Introduction to Inheritance . What is Inheritance? Superclass vs. Subclass Why Inheritance? Subclass Syntax Final Classes Class Hierarchy Subclass Constructors Example Exercises. What is Inheritance? . Parent. a b. op1( ) op2( ). Child. c. op3( ).
E N D
Introduction to Inheritance • What is Inheritance? • Superclass vs. Subclass • Why Inheritance? • Subclass Syntax • Final Classes • Class Hierarchy • Subclass Constructors • Example • Exercises
What is Inheritance? Parent a b op1( ) op2( ) Child c op3( ) • 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: op1( ) , op2( ) • not inherited: op3( ) • 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.
Superclass vs. Subclass Course Student OnLineCourse ShortCourse GraduateStudent deliverLecture( ) produceCertificate( ) defendThesis( ) • 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.
isA relationship Course Student OnLineCourse ShortCourse GraduateStudent deliverLecture( ) produceCertificate( ) defendThesis( ) • A subclass-superclass pair defines “isA” 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.
Why Inheritance? • Through inheritance we gain software reuse. • Helps in writing structured programs. • It is more natural and closer to real world.
Subclass Syntax • class SubclassName extends SuperclassName • { • variables • methods • } • Example: • class GraduateStudent extends Student • { • String thesisTitle; • . . . • defendThesis(){. . .} • . . . • }
Final Classes • final class className • { • variables • methods • } • A class that is declared as final cannot be extended or subclassed. • Examples of final classes are: • java.lang.System. • The primitive type wrapper classes: Byte, Character, Short, Integer, Long, Float, and • Double.
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:
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 GraduateStudent(int id, String name, double gpa, String thesisTitle){ super(id, name, gpa); this.thesisTitle = thesisTitle; } • 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.
Exercises Question 1: What inheritance relationships would you establish among the following classes:Student, Professor, Teaching Assistant, Employee, Secretary, DepartmentChairman, Janitor, Person, Course, Seminar, Lecture, ComputerLab Question 2: In the following pairs of classes, identifythe superclass and the subclass: Manager -Employee, Polygon -Triangle, Person - Student, Vehicle - Car, Computer-Laptop, Orange - Fruit.