Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
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.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
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.
Multiple Choice Solutions True/False a c b e d   T F.
Polymorphism & Interfaces
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
M1G Introduction to Programming 2 5. Completing the program.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
BY:- TOPS Technologies
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Object Oriented Programming
Chapter 19 Generics Dr. Clincy - Lecture.
Java – Inheritance.
Unit 3 Test: Friday.
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Review: libraries and packages
2009 Test Key.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends Book Open Book, Dictionary, InheritanceClient Note that a client that creates a Dictionary object can actually call a method that only exists in the Book class. What if a method exists in both the parent and the child, but has the same exact name? This is called method overriding. The child class overrides its parent’s method, which allows it to accomplish a similar task, but in its own way.

Polymorphism What if you know you want to call a method, but you are not sure yet if you want to call it from the parent or from the child? In this case, you can create an object of the parent class, but instantiate it as an object of the child class. This is called polymorphism. Examples: 1) Vehicle x = new Car(); 2) List z = new ArrayList( ); 3) Vehicle y; y = new Truck(); Notice that in each of these examples, the order of declarations goes parent  child. Never the reverse. When an overridden method in one of those classes is then called, how does Java know which one to actually call? Java decides this at run-time. This run-time decision by Java is called dynamic binding.

What is the point of polymorphism? Often, it makes sense that subclasses (child classes) of a superclass (parent class) can define their own unique behaviors, and yet share some of the same functionality of the parent class. The programmer may wish to call the parent’s method, or one child’s method, or even a different child’s method. (Remember, these are all methods with the same name.) So, polymorphism allows that choice to be made later on.

Open: – Animal – Dog – Cat – PolymorphismClient

Polymorphism is commonly seen when using ArrayList. ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor of the List interface, called LinkedList, that you do not have to know about, but it helps to be aware of it. However, you do need to be aware of the List interface. Often, you will see ArrayLists declared this way: List x; And then, later on: x = new ArrayList(); Or, a later line of code might want to change x to be a LinkedList. If it had been declared as an ArrayList originally, this would be a problem. Note: we learned previously that an interface cannot be instantiated. In this example, we are not instantiating List; we are actually instantiating ArrayList.

Static variables Remember: a static method is a method that can be called from a client without having to create an object. Just use the name of the class itself (examples: Math.random(), SavitchIn.readLine() ). A variable can also be declared with the keyword static. Just like a static method, a static variable is not accessed through an object. It is accessed by using the name of the class itself. Why use a static variable? Sometimes, you want a variable to be shared among all instances of an object, as opposed to each object having its own version of that variable. A static variable is also known as a class variable. Example: see StaticMethodClass & StaticMethodClient

Arrays of Objects An array is not limited to holding just ints, or chars, or Strings, etc. An array can hold objects. Remember, when you connect to (instantiate) a class, you create an object, for example: Numbers x = new Numbers(); In this example, x is an object. Demo: Person, ArrayOfObjectsDemo

Types of errors There are 3 types of errors: – Compiler error: An error that the compiler will point out to you before you can run it – Run-time error: Will not be noticed by the compiler; will crash the program when it runs – Logic Error: does not cause either of the above errors, but makes your program run in a way you do not want it to

Restricting the values in an Arraylist By default, an ArrayList can store any type of Object. You can, if you wish, restrict the type of thing that an ArrayList can hold, by using the symbols. This is called using generics. Examples: ArrayList z = new ArrayList (); ArrayList x = new ArrayList (); List a; a = new ArrayList ();

Why use generics? Simply put, using generics will make your code more “stable” by catching more errors at compile time rather than at run-time. Compiler errors are usually easier to fix than runtime errors. When the compiler knows that an ArrayList is restricted to holding Strings, for example, it can check your code before running the program to find any errors that specifically involve using Strings. Another benefit of using generics in an ArrayList is that it eliminates the need for type casting. Example: ArrayList nums = new ArrayList (); nums.add(23); nums.add(11); // no need for “new Integer (11)” etc

Another example: public class Sample { private ArrayList ; // more code here } In this class, a private instance variable is created – it is an Arraylist. But note that it will NOT hold objects of the Comparable interface, because that is not valid. An interface cannot be instantiated. Instead, it will hold objects that implement the Comparable interface.

(ItchyAndScratchy) – Create an 11-by-11 board which is filled with dashes. – Create a class, ItchyClass, containing one method, DisplayBoard. This method should receive the board as a parameter, display it, and return nothing. – Randomly place an I and an S on the board. – Allow the user to control the I (Itchy) by typing L, R, U, or D, for each of the 4 directions. This will move the I one space on the board. – When Itchy catches Scratchy, the game is over. Display how many moves it took. – Allow the option to play again. – Optional: Allow the user to choose a difficulty level, in which Scratchy (the computer) can move too.