Class Inheritance Liang, Chpt 8. X is a Y: the is-a hierarchy life-form animal person clown geometric object circle cylinder instrument trumpet piano.

Slides:



Advertisements
Similar presentations
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
Advertisements

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
UFCE3T-15-M Programming part 1 UFCE3T-15-M Object-oriented Design and Programming Block2: Inheritance, Polymorphism, Abstract Classes and Interfaces Jin.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Liang Chapter 6 Variable scope and the keyword this.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
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.
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.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
What is inheritance? It is the ability to create a new class from an existing class.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
 Definition: The ability to derive child classes (sub- classes) from parent classes (super-classes)  Characteristics:  Methods and instance variables.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 4 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
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.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Chapter 9 Inheritance and Polymorphism. Object-oriented programming is based on a paradigm in which objects are used to model a specification. Objects.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance.
Class Inheritance Part I
Lecture 12 Inheritance.
One class is an extension of another.
Polymorphism 11-Nov-18.
One class is an extension of another.
Polymorphism 21-Apr-19.
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Class Inheritance Liang, Chpt 8

X is a Y: the is-a hierarchy life-form animal person clown geometric object circle cylinder instrument trumpet piano

Superclass/Subclass piece of furniture sofa superclass subclass (base class, parent class) (child class, derived class)

Inheritance An instance of a subclass is an instance of its superclass A subclass inherits methods and variables from its superclass A subclass adds to the superclass: it is an extension of the superclass

Reminder: the Circle class public class Circle { private double radius; public Circle()..... public Circle(double r).... public double getRadius().... public void setRadius(double newRadius).... public double findArea() }

Cylinder as a special kind of Circle public class Cylinder extends Circle { private double length; public double getLength().... public void setLength(double newLength)... public double findVolume()... } + constructors

Reminder: constructors for Circle // default constructor public Circle() { radius = 1.0; } // Construct Circle with specific radius public Circle(double r) { radius = r; }

Constructors for Cylinder // default constructor public Cylinder() { super(); length = 1.0; } // Constructor with expicit length and radius public Cylinder(double r, double l) { super(r); length = l; }

Using a Cylinder Cylinder myCyl = new Cylinder(5.0, 2.0); System.out.println("length: " + myCyl.getLength()); System.out.println("radius: " + myCyl.getRadius()); System.out.println("volume: " + myCyl.findVolume()); System.out.println("area: " + myCyl.findArea()); Program TestCylinder, Example 6.1

UML diagram Circle -radius +getRadius +setRadius +findArea Cylinder -length +getLength +setLength +findVolume

Reminder: using this in methods this refers to the present object class Foo { int i = 5; void setI(int i) { this.i = i; } "Assign argument i to the object's instance variable i "

Reminder: using this(..) in constructors this(...) in a constructor calls another constructor for this class public Circle(double radius) { this.radius = radius; } public Circle() { this(1.0); } Note: a call to this(...) must be the first statement in the constructor

Using super Like this, super can be used to call a method or to call a constructor If used like super.findArea(), it calls the findArea method of the superclass If used like super(1.0), it calls the corresponding constructor of the superclass If used as super(1.0) in a constructor, it must be the first statement of the constructor.

Example: using super in a constructor // default constructor public Cylinder() { super(); // call default const. of superclass this.length = 1.0; } // another constructor public Cylinder(double r, length l) { super(r); this.length = l; }

Overriding methods Sometimes a subclass may choose to re- define a method which it inherits If it does so, the new method (e.g. in Cylinder) overrides the corresponding method of the superclass (Circle) E.g. findArea() in class Cylinder should really return the surface area of the cylinder

Example of overriding // in class Cylinder public double findArea() { return 2 * super.findArea() + (2*getRadius()*Math.PI) * length; }

Another toy example Instrument: "toot toot" Saxophone extends Instrument: "honk honk" (overrides play) Trumpet extends Instrument: calls super.play() and adds to it

Object class Every class has a superclass If a class does not explicitly extend another class, its superclass is Object Class Object defines a few, generic methods These methods should usually be overridden by subclasses (by all classes)

Hierarchy again Object Instrument Saxophone Trombone Circle Cylinder

Methods inherited from Object public boolean equals(Object O) public String toString() and others (clone, etc)

The equals method Purpose: to test whether two objects of the same type are "equal" in some meaningful sense Default behaviour: test for equality of references Example of overridden method: String.equals Exercise: add an equals method to Circle Habit for life: always define an equals method

The toString method Purpose: return a String representation of an object, e.g. for printing to the console Default behaviour: print classname and address of the object reference Better behaviour: print something sensible and informative Example: Circle[radius=1.0] Exercise: examine default behaviour and modify it Most common use: implicit call in String concatenation

Implicit calling of toString() The following two groups of statements are equivalent: Circle c = new Circle(); String s = c.toString(); System.out.println("Here: " + s); Circle c = new Circle(); System.out.println("Here: " + c);