Introduction to Inheritance

Slides:



Advertisements
Similar presentations
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Advertisements

CS 211 Inheritance AAA.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Intro to OOP with Java, C. Thomas Wu
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
What is inheritance? It is the ability to create a new class from an existing class.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
Unit-2 Objects and Classes
Advanced Programming Behnam Hatami Fall 2017.
Presentation transcript:

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

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: 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. Parent Child a b op1( ) op2( ) c op3( )

Superclass vs. Subclass 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 OnLineCourse deliverLecture( ) ShortCourse produceCertificate( ) Student GraduateStudent defendThesis( )

isA relationship 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. Course OnLineCourse deliverLecture( ) ShortCourse produceCertificate( ) Student GraduateStudent defendThesis( )

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.

Example class Vehicle{ private String vehicleIdNumber ; private String vehicleChassisNumber ; private String model ; public Vehicle(String vin, String vcn, String model){ vehicleIdNumber = vin; vehicleChassisNumber = vcn; this.model = model; } public String toString( ){ return "Vehicle ID = " + vehicleIdNumber + "\nVehicle Chassis Number = " + vehicleChassisNumber + "\nVehicle Model = " + model; public boolean equals(Vehicle vehicle){ return this.vehicleChassisNumber == vehicle.vehicleChassisNumber; class Bus extends Vehicle{ private int numberOfPassengers ; public Bus(int numPassengers, String vin, String vcn, String model){ super(vin, vcn, model) ; numberOfPassengers = numPassengers ; public int getNumberOfPassengers( ){ return numberOfPassengers ;

Example (cont’d) class Truck extends Vehicle{ private double cargoWeightLimit ; public Truck(double weightLimit, String vin, String vcn, String model){ super(vin, vcn, model) ; cargoWeightLimit = weightLimit ; } public double getCargoWeightLimit( ){ return cargoWeightLimit ; public class VehicleTest{ public static void main(String[] args){ Vehicle vehicle = new Vehicle ("QMY 489", "MX-0054322-KJ", "BMW 500"); Bus bus1 = new Bus(30, "TMK 321", "AF-987654-WR", "MERCEDEZ BENZ"); Bus bus2 = new Bus(30, "2348976", "AF-987654-WR", Truck truck = new Truck(10.0, "DBS 750", "RZ-70002345-PN", "ISUZU"); System.out.println(vehicle) ; System.out.println(bus1) ; System.out.println(truck) ; if(bus1.equals(bus2)) System.out.println("Bus1 and Bus2 are the same") ; else System.out.println("Bus1 and Bus2 are not the same") ;

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, identify the superclass and the subclass: Manager -Employee, Polygon -Triangle, Person - Student, Vehicle - Car, Computer-Laptop, Orange - Fruit.