Inheritance. Inheritance  New class (derived class) is created from another class (base class).  Ex. EmployeeEmployee  HourlyEmployee  SalariedEmployee.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Object Oriented Programming with Java
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Object class.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
What is inheritance? It is the ability to create a new class from an existing class.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
CMSC 202 Inheritance I Class Reuse with Inheritance.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Lecture 12 Inheritance.
Object-oriented Programming in Java
Chapter 11 Inheritance and Polymorphism
Polymorphism 2nd Lecture
Java Inheritance.
Lecture 17: Polymorphism (Part II)
Computer Science II Exam 1 Review.
Overloading and Constructors
Inheritance 2nd Lecture
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Polymorphism 2nd Lecture
Comp 249 Programming Methodology
Inheritance I Class Reuse with Inheritance
Comp 249 Programming Methodology
CMSC 202 Inheritance.
Inheritance.
Class Reuse with Inheritance
Inheritance 2nd Lecture
Abstract methods and classes
Polymorphism.
Lecture 18: Polymorphism (Part II)
Object class.
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 7 Inheritance.
Presentation transcript:

Inheritance

Inheritance  New class (derived class) is created from another class (base class).  Ex. EmployeeEmployee  HourlyEmployee  SalariedEmployee Some things like name and hireDate are common to all employees.Some things like name and hireDate are common to all employees.  Belong in a base class  What else can you imagine belongs in the base class?

Inheritance  Employee HourlyEmployeeHourlyEmployee SalariedEmployeeSalariedEmployee  Some things are relevant to only a salaried employee or an hourly employee. Can you think of anything?Can you think of anything?

Employee public class Employee { private StringmName; private DatemHireDate; public Employee ( ) { … } public Employee ( String name, Date date ) { … } public Employee ( Employee original ) { … } public String getName ( ) { … } public Date getHireDate ( ) { … } public void setName ( String name ) { … } public void setHireDate ( Date date ) { … } public String toString ( ) { … } public boolean equals ( Employee other ) { … } }

Inheritance syntax public class DerivedClassName extends BaseClassName {DeclarationsOfAddedStaticVariablesDeclarationsOfAddedInstanceVariablesDefinitionsOfAddedAndOverriddenMethods}

Inheritance steps 1. Define base class (e.g., Employee) 2. Define derived class public HourlyEmployee extends Employee { …}

Inheritance  Base class = parent class = ancestor = superclass  Derived class = child class = descendent = subclass  Public and protected attributes and methods in the base class are available to (inherited by) the derived class.

Overriding methods  Say our HourlyEmployee class adds an hourlyRate attribute.  Say the base class has toString and equals methods.  The derived class inherits these but they aren’t 100% sufficient for the derived class.  What do we do?

Overriding methods  What do we do? Reimplement the equals and toString methods in the derived class.Reimplement the equals and toString methods in the derived class. These reimplemented methods override the methods in the base class.These reimplemented methods override the methods in the base class.

Overriding methods  Note: What does the keyword final mean when used with instance variables?What does the keyword final mean when used with instance variables? What does the keyword final mean when used with methods?What does the keyword final mean when used with methods? What does the keyword final mean when used with the class definition?What does the keyword final mean when used with the class definition? How does overriding differ from overloading?How does overriding differ from overloading?

Covariant return type  In general, one cannot override a method and change the return type.  Exception: (Java 5 or greater) If the return type is a class type, it can be changed to a descendent type of the original class type.If the return type is a class type, it can be changed to a descendent type of the original class type.

Public and private  Derived class can “loosen” access restrictions. Derived class can change private to public.Derived class can change private to public.  Derived class can’t “tighten” them. Derived class can’t change public to private.Derived class can’t change public to private.

Super ctor  Subclass ctor may call superclass ctor. Ex. super( a, b, … );Ex. super( a, b, … ); Number of args may differNumber of args may differ Call to super() must be first line in subclass ctor.Call to super() must be first line in subclass ctor.  If derived class doesn’t call a base class ctor, then the no-arg ctor of the base class is automatically invoked (first). How can we demonstrate this?How can we demonstrate this?  Typically, a base class ctor should always be called.

Super ctor  If derived class doesn’t call a base class ctor, then the no-arg ctor of the base class is automatically invoked (first). How can we demonstrate this?How can we demonstrate this? public class Tester7 extends Tester7Superclass { public Tester7 ( String s ) { public Tester7 ( String s ) { System.out.println( "Tester7() says hello." ); } public static void main ( String p[] ) { new Tester7( "fred" ); }} class Tester7Superclass { public Tester7Superclass ( ) { System.out.println( "Tester7Superclass() says hello." ); }} How can we make sure that this doesn’t happen?

Super ctor  If derived class doesn’t call a base class ctor, then the no-arg ctor of the base class is automatically invoked (first). How can we demonstrate this?How can we demonstrate this? public class Tester7 extends Tester7Superclass { public Tester7 ( String s ) { public Tester7 ( String s ) { System.out.println( "Tester7() says hello." ); } public static void main ( String p[] ) { new Tester7( "fred" ); }} class Tester7Superclass { private Tester7Superclass ( ) { System.out.println( "Tester7Superclass() says hello." ); }} How can we make sure that this doesn’t happen? Won’t even compile!

The this ctor  We can use the this ctor to call another ctor in the same class. Must call the this ctor firstMust call the this ctor first Can’t use both the this ctor and super.Can’t use both the this ctor and super. Ex.Ex. public HourlyEmployee ( ) { this( “no name”, new Date(“January”, 1, 1000), 0, 0 ); }

Types  An object of a derived class has more than one type. Not only its type but also the type of every one of its ancestors (all the way back to Object).Not only its type but also the type of every one of its ancestors (all the way back to Object). Object is the base class for classes that don’t extend anything. Object is the ancestor of all classes.Object is the base class for classes that don’t extend anything. Object is the ancestor of all classes.

java.lang.Object  Many methods.  See /api/java/lang/Object.html. /api/java/lang/Object.html /api/java/lang/Object.html

Types  instanceof operator Object instanceof ClassNameObject instanceof ClassName True if Object is of type ClassName; false otherwiseTrue if Object is of type ClassName; false otherwise Example:Example: Integer i = new Integer(12); Object o = i; //won’t compile (not possible): //System.out.println( (i instanceof String) ); //OK. Compiles; runs; returns false: System.out.println( (o instanceof String) ); //false System.out.println( (o instanceof Object) ); //true System.out.println( (o instanceof Integer) ); //true