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.
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.
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.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
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.
©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.
(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.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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)
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.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
BY:- TOPS Technologies
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
Advanced Programming in Java
Overriding Method.
Polymorphism, Abstract Classes & Interfaces
Interfaces.
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Inheritance in Java.
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.
ATS Application Programming: Java Programming
Object Oriented Programming
Chapter 19 Generics Dr. Clincy - Lecture.
Polymorphism, Abstract Classes & Interfaces
Java – Inheritance.
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Review: libraries and packages
2009 Test Key.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Advanced Programming in Java
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

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.