Overriding Method.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
BY:- TOPS Technologies
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
EKT472: Object Oriented Programming Overloading and Overriding Method.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance.
Polymorphism.
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 9 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
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:

Overriding Method

The Object Class Every class in Java is descended from the java.lang.Object class. If no inheritance is specified when a class is defined, the superclass of the class is Object.

Declaring a Subclass A subclass extends properties and methods from the superclass. You can also: Add new properties Add new methods Override the methods of the superclass

The toString() method in Object The toString() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object. Circle circ = new Circle(); System.out.println(circ.toString()); The code displays something like Circle@82ba41 . This message is not very helpful or informative. Usually you should override the toString method so that it returns a digestible string representation of the object.

Example 1: By default, all Java classes are subclasses of the Object class (the most general class in Java’s hierarchy). Object + toString(): String Student # name : String + Student() + Student(s:String) + getName(): String One public method that is defined in the Object class is the toString()method.

Example 1: public class Student { protected String name; public Student () { } public Student (String s) { name = s; } public String getName() { return name; } } public class TestStudent1 { public static void main( String args[]){ Student stu = new Student("Ana"); System.out.println (stu.toString()); }

Example 1: output The default implementation of toString() Student@82ba41 Name of the object Address of the object The default implementation of toString() Returns the name of the object’s class and the address where the object is stored in the memory.

Example 2: Overriding an Inherited Method Object + toString(): String Student # name : String + Student() + Student(s:String) + getName(): String + toString():String toString()method is redefined in subclasses of Object class. Overriding toString()in a Subclass provides a customized string representation of the Object in that subclass.

Example 2 public class Student { protected String name; public Student () { } public Student (String s) { name = s; } public String getName() { return name; public String toString() { return "My name is " + name + " and I am a Student.";

Example 2 public class TestStudent2 { public static void main( String args[]){ Student stu = new Student("Ana"); System.out.println (stu.toString()); } Output: My name is Ana and I am a Student.

Overriding Methods in the Superclass Any time you have a class that inherits a method from a superclass, you have the opportunity to override the method (unless, the method is marked final). The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type. public class Animal { public void eat() { System.out.println("Generic Animal Eating Generically"); } class Horse extends Animal { System.out.println("Horse eating hay, oats, " + "and horse treats"); public void buck() { System.out.println ("This is how I jump");

Overriding Methods in the Superclass public class TestAnimals { public static void main (String [] args) { Animal a = new Animal(); Animal b = new Horse(); //Animal ref, but a Horse object a.eat(); // Runs the Animal version of eat() b.eat(); // Runs the Horse version of eat() } The Animal class creator might have decided that for the purposes of polymorphism, all Animal subtypes should have an eat() method defined in a unique, specific way. Polymorphically, when someone has an Animal reference that refers not to an Animal instance, but to an Animal subclass instance, the caller should be able to invoke eat() on the Animal reference, but the actual runtime object (say, a Horse instance) will run its own specific eat() method.

Overriding Methods in the Superclass public class TestAnimal2 { public static void main (String [] args) { Animal c = new Horse(); //Animal ref, but a Horse object c.buck(); } The TestAnimal2 class uses an Animal reference to invoke a method on a Horse object. Remember, the compiler will allow only methods in class Animal to be invoked when using a reference to an Animal. Can't invoke buck(); because Animal class doesn't have that method.

Overriding Methods in the Superclass A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. This is referred to as method overriding. public class Circle extends GeometricObject { // Other methods are omitted /** Override the toString method defined in GeometricObject */ public String toString() { return super.toString() + "\nradius is " + radius; }

Invoking a Superclass Version of an Overridden Method The keyword super can also be used to reference a method in the superclass. public class Animal2 { public void eat() { } public void printYourself() { // Useful printing code goes here } class Horse extends Animal2 { // Take advantage of Animal code, then add some more super.printYourself(); // Invoke the superclass // (Animal) code // Then do Horse-specific // print work here

Dynamic Binding A method call is bound to the correct implementation of the method at runtime by the Java Virtual Machine (JVM). When JVM encounters a method call, it uses information about the class hierarchy to bind the method call to the correct implementation of that method. Java’s dynamic-binding mechanism, which is also called late binding, or runtime binding, leads to what is known as polymorphism.

NOTE An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class.

A private method cannot be overridden public class TestAnimals3 { public static void main (String [] args) { Horse h = new Horse(); h.eat(); } class Animal { private void eat() { System.out.println("Generic Animal Eating Generically"); class Horse extends Animal { } Method eat() in the superclass can’t be inherited, because it is private. Therefore, method eat() cannot be overridden.

NOTE If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.

A private method cannot be overridden public class TestAnimals2 { public static void main (String [] args) { Animal a = new Animal(); Animal b = new Horse(); //Animal ref, but a Horse object a.eat(); // Runs the Animal version of eat() b.eat(); // Runs the Horse version of eat() } class Animal { public void eat() { System.out.println("Generic Animal Eating Generically"); class Horse extends Animal { private void eat() { // whoa! - it's private! System.out.println("Horse eating hay, oats, " + "and horse treats");