“The beauty of empowering others is that your own power is not diminished in the process.” – Barbara Colorose Thought for the Day.

Slides:



Advertisements
Similar presentations
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
“The task of leadership is not to put greatness into people, but to elicit it, for the greatness is there already.” – John Buchan Thought for the Day.
Mark Dixon, School of Computing SOFT 120Page Object Associations (part 2): Inheritance and Polymorphism Priestley (2000) sections 6.5, 6.6, and 6.7.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
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.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
1 University of Sunderland Advanced OO Development “Building a small bank system” © James Malone, 2003.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Topic 4 Inheritance.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
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…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
© 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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
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.
Object-Oriented Programming: Polymorphism Chapter 10.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Programming in Java: lecture 7
Lecture 12 Inheritance.
One class is an extension of another.
Inheritance and Encapsulation
CS100A Lecture 22 Previous Lecture This Lecture Inheritance
CSC 205 Java Programming II
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
Encapsulation Inheritance PolyMorhpism
Chapter 7 Polymorphism.
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Inheritance and Polymorphism
Java Inheritance.
Advanced Inheritance Concepts
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

“The beauty of empowering others is that your own power is not diminished in the process.” – Barbara Colorose Thought for the Day

Test Tomorrow 3 March 2011

Test Covering: –Chapters 1 – 11 (More Classes and Objects) Format: –40 marks aim for mark per minute –Short answer/multiple choice section –Short programming questions

Questions Demonstrate your knowledge of the syntax and semantics of Java, and the important underlying concepts Be able to understand and to write methods and short code fragments –Not full programs –Possibly outline what needs to be in a class (design)

Chapter 11 More About Classes Inheritance

Overriding toString public class ChequeAccount extends Account {... public String toString () { return (accountNumber + ” bal = R” + balance); } // toString } // class ChequeAccount

The equals Method Compares two objects –Default: compares the references (same as == ) Can override –e.g. compare account numbers

Overriding equals public class ChequeAccount extends Account {... public boolean equals (Object other) { Account a = (Account)other; return accountNumber.equals(a.accountNumber); } // equals } // class ChequeAccount

Polymorphism It’s Greek to me! Objects can take on different forms: ChequeAccount c = new ChequeAccount(); Account a = new ChequeAccount(); ?

Polymorphism (cont.) The ChequeAccount object has taken on the form of an Account object It is still a ChequeAccount object Account a = new ChequeAccount(); a ChequeAccount

Polymorphism (cont.) Inheritance is the key: –Any object can take on the form of any of its superclasses Throwable t = new FileNotFoundException();

The type of the reference variable determines what we can do with object –Using polymorphism we lose access to more specialised features Polymorphism (cont.) We cannot access any overdraft information using a Account a = new ChequeAccount();

Overridden methods The type of the object determines which method is called Polymorphism (cont.) The ChequeAccount withdraw method is used –allows overdraft Account a = new ChequeAccount(); a.withdraw(100);

Why Do We Use Polymorphism? Generic methods Generic lists

Generic Methods This method can work with Account objects or any subclass of the Account class: –ChequeAccount –FixedDepositAccount –CreditCardAccount –etc. public void writeBalance (Account acc) { logFile.println(acc.accountNumber + ": R" + acc.balance); } // writeBalance

Generic Lists This array can hold any object Object list[] = new Object[100]; list[0] = ”George”; list[1] = new ChequeAccount(); list[2] = new Integer(-73); list[3] = new double[5]; list[4] = System.in;...