Lecture 9_1 George Koutsogiannakis

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Reusable Classes.  Motivation: Write less code!
CS 211 Inheritance AAA.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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,
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
1 CS2200 Software Development Lecture 29: Polymorphism I A. O’Riordan, 2008 Based on notes by K. Brown.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 12 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 12 GEORGE KOUTSOGIANNAKIS
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 2 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II GEORGE KOUTSOGIANNAKIS
Inheritance in Java.
Lecture 10: Inheritance Subclasses and superclasses
An Introduction to Inheritance
CS100A Lecture 22 Previous Lecture This Lecture Inheritance
Week 8 Lecture -3 Inheritance and Polymorphism
Comparing Objects Phil Tayco San Jose City College Slide version 1.1
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Object Oriented Programming (OOP) LAB # 8
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Computer Programming with JAVA
Java – Inheritance.
Chapter 11 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II GEORGE KOUTSOGIANNAKIS
Recitation 7 October 7, 2011.
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS
Chapter 10: Method Overriding and method Overloading
Method of Classes Chapter 7, page 155 Lecture /4/6.
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Parameters, Overloading Methods, and Random Garbage
Presentation transcript:

Lecture 9_1 George Koutsogiannakis CS 116 Lecture 9_1 George Koutsogiannakis

TOPIC Method Overriding Method Overloading

Method Overriding Sometimes the same method is used to produce two different results in two different classes i.e: Assume that method m is in super class A and it has signature: public int m(double d) { method code to produce a task} Suppose that class B is sub class of A (inherits A). As a result it has inherited the method m of A also. Suppose that class B is interested in adding code to m (and thus modify the original code) but it wants the same signature as m (same name, same returned value, same arguments). B can go ahead and modify the method m which is different than the m of the super class. This is called method overriding i.e

Method Overriding i.e. Class B version of m: public int m(double d1) { int x = super.m(3.4); // the above is a call to the A class version to get its code //Add additional code here i.e. int y=(int)(d1+x); return y; }

Method Overriding The client class will go ahead and access m with an object of B: B b=new B(); int i= b.m(5.0);

Method Overloading Method overloading happens when a class decides to create a second version of a method that exists in the same class but with different list of arguments i.e. In our previous example the lass A had the method m with signature: public int m(double d) { method code to produce a task} Class A also decides that it wants to have different versions of m but this time the signature will also be affected by the fact that the list of argument is different i.e. public int m(double d, String str) { method code to produce a slightly different task} This is called method overloading (notice that both versions of m exist in the same class A)

Method Overloading The user of class A decides which version of m wants to use simple by indicating the arguments in m i.e. A a= new A(); int x=a.m(2.4); int y=a.m(3.4, “a string”); An good example of method overloading is the different constructors in a class!