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.

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Java Class and Inheritance.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Tirgul no. 9 Topics covered: H Inheritance, the basics: - Inheritance Trees - base class vs. sub class. - Constructor invocation. - Overriding vs. Overloading.
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.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Understanding Inheritance CompSci 230 S Software Design and Construction.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Peyman Dodangeh Sharif University of Technology Fall 2014.
COS 312 DAY 13 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress Over due – Next progress report is March 26 Assignment 4 Posted – Chap 6 & 7 – Due.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Inheritance Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Object Oriented Concepts Recap OOP. The four Pillars of OOP Abstraction Encapsulation Inheritance Polymorphism.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
/** Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[]
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Inheritance Chapter 8 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Chapter 8 Inheritance.
COS 312 DAY 15 Tony Gauvin.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 8 Inheritance Part 2.
Chapter 11 Inheritance and Polymorphism Part 1
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

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 the same name but different signatures b Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature b Overloading lets you define a similar operation in different ways for different data b Overriding lets you define a similar operation in different ways for different object types

2 Class Hierarchies b A child class of one parent can be the parent of another child, forming class hierarchies StaffMember VolunteerEmployee ExecutiveHourly

3 Indirect Access I b An inherited member can be referenced directly by name in the child class, as if it were declared in the child class b But even if a method or variable is not inherited by a child, it can still be accessed indirectly through parent methods

4 Indirect Access II class FoodItem { final private int CALORIES_PER_GRAM = 9; private int fatGrams; protected int servings; public FoodItem (int fatGrams, int servings) { this.fatGrams = fatGrams; this.servings = servings; } private int calories () { return fatGrams * CALORIES_PER_GRAM; } public int caloriesPerServing () { return (calories() / servings); }

5 Indirect Access III class Pizza extends FoodItem { public Pizza (int fatGrams) { super (fatGrams, 8); } b The following code works: Pizza special = new Pizza (275); System.out.println ("Calories per serving: " + special.caloriesPerServing());

6 Inheritance Example class Point { public Point( int x, int y) { setPoint(x, y); } public Point() { this(0, 0); } public void setPoint( int x, int y) { this.x = x; this.y = y; } public String toString() { return “(“ + x + “,” + y + “)”; } public int getX() {return x; } // get x coordinate public int getY() { return y; } // get y coordinate protected int x,y; // accessible by derived classes }

7 class Circle extends Point { private double radius; public Circle(int x, int y, double r) { super(x, y); setRadius( r ); } public Circle() { // done automatically anyway this(0, 0, 0); } public void setRadius( double r ) { radius = ( r >= 0 ? r : 0 ); } public double getRadius() { return radius; } public double getArea() { return Math.PI* radius * radius; } public String toString() { return “(“ + super.toString() + “,” + radius + “)”; } Example (cntnd.)

8 class Cylinder extends Circle { private double height; public Cylinder(double h, double r, int x, int y ) { super( r, x, y ) // call base-class constructor setHeight( h ); } public void setHeight( double h ) { height = h > 0 ? h : 0; } public double getHeight() { return height; } public double getArea() { return 2 * super.area() + 2 * Math.PI * getRadius() * height; } public double getVolume() { return super.area() * height; } public String toString() { return “(“ + super.toString() + “,” + height + “)”; }

The class hierarchy: Point PixelCircle Cylinder

10 Final classes and final methods b Final class, why? - increase system security: disables creating a subclass of a class and then substituting for the original one - good object-oriented design: your class should have no subclasses  To protect some of your class's methods from being overridden declare them final. - synchronization in java.lang.Object - methods of java.io. DataInputStream

11 Constructor Invocation b b The constructors are called either explicitly or implicitly from base-class to sub-class down the inheritance hierarchy. b The compiler forces invocation of base-class constructor as the first thing that happens in the sub-class constructor, this means you cannot catch any exception thrown in the base-class's constructor.

12 Inheritance hierarchy public class NoInheritence { public NoInheritence() { System.out.println("No Inheritance."); } public class OneInheritence extends NoInheritence { public OneInheritence() { System.out.println("One Inheritance."); } public class TwoInheritence extends OneInheritence { public TwoInheritence() { System.out.println("Two Inheritance."); } public static void main(String args[]) { TwoInheritence ti = new TwoInheritence(); }