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.

Slides:



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

Polymorphism Method overriding Method overloading Dynamic binding 1.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
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.
Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
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 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
More about inheritance Exploring polymorphism 3.0.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Chapter 2: Java Fundamentals
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
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.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance ndex.html ndex.htmland “Java.
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Polymorphism.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Overriding Method.
Inheritance and Polymorphism
Methods Attributes Method Modifiers ‘static’
Programming Language Concepts (CIS 635)
Web Design & Development Lecture 5
ATS Application Programming: Java Programming
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding
More inheritance, Abstract Classes and Interfaces
Extending Classes.
CSC 113: Computer programming II
More about inheritance
CMSC 202 Polymorphism.
METHOD OVERRIDING in JAVA
Java – Inheritance.
Sampath Kumar S Assistant Professor, SECE
Inheritance Cse 3rd year.
Java Inheritance.
Polymorphism.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Final Jim Brucker.
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Inheritance and Polymorphism Part 1
Debugging Exercise 00 Try compiling the code samples.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

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 , same return type and may have either the same or higher scope than super class method. Java implements Run Time Polymorphism/ Dynamic Method Dispatch by Method Overriding. [Late Binding] Call to Overridden Methods is Resolved at Run Time. Call to a overridden method is not decided by the type of reference variable Rather by the type of the object where reference variable is pointing. While Overriding a Method, the sub class should assign either same or higher access level than super class method.

EXAMPLE METHOD OVERRIDING class A { void show() System.out.println("Hello This is show() in A"); }// End of show() Method } // End of class A class B extends A System.out.println("Hello This is show() in B"); } // End of class B B class overrides show() method from super class A class override { public static void main(String args[]) // super class reference variable // can point to sub class object A a1 = new A(); a1.show(); a1 = new B(); } Call to show() of A class Call to show() of B class

Hello This is show() in A Hello This is show() in B Is this Method Overriding class A { void show(int a) System.out.println("Hello This is show() in A"); } class B extends A void show() System.out.println("Hello This is show() in B"); class override1 { public static void main(String args[]) /* A a1 = new B(); a1.show(); */ A a1 = new A(); a1.show(10); B b1 = new B(); b1.show(10); b1.show(); } NO OUTPUT Hello This is show() in A Hello This is show() in B

Dynamic Method Dispatch Super class reference variable can refer to a sub class object. Super class variable if refers to sub class object can call only overridden methods. Call to an overridden method is decided by the type of object referred to. A A a1 = new B(); a1.show(); // call to show() of B a1 = new C(); a1.show(); // call to show() of C a1 = new D(); a1.show(); // call to show() of D B C D Assume show() Method is Overridden by sub classes

DYNAMIC METHOD DISPATCH class A { void show() { System.out.println("Hello This is show() in A"); } } class B extends A { void show() { System.out.println("Hello This is show() in B"); } } class C extends A { void show() { System.out.println("Hello This is show() in C"); } } class D extends A { void show() { System.out.println("Hello This is show() in D"); } } CONTINUED…..

Hello This is show() in A Hello This is show() in B class override2 { public static void main(String args[]) A a1 = new A(); a1.show(); a1 = new B(); a1 = new C(); a1 = new D(); } Hello This is show() in A Hello This is show() in B Hello This is show() in C Hello This is show() in D

class override3 { public static void main(String args[]) A a1 = new B(); B b1 = (B) a1; /* C c1 = (C) a1; Exception in thread "main" java.lang.ClassCastException: B at override3.main(override3.java:39) */ }

Examples Overriding A a1 = new B(); class A a1.show() ; // Valid { // a1.show(10); // Invalid //a1.print(); // Invalid class A { void show() { …. } } class B extends A void show(int x) { … } void print() { … } When a super class variable points to a sub class object, then it can only call overridden methods of the sub class.

class A { protected void show() System.out.println("Hi"); } class B extends A void show() D:\Java1>javac AB.java AB.java:10: show() in B cannot override show() in A; attempting to assign weaker access privileges; was protected void show() ^ 1 error

NO IS THIS METHOD OVERRIDING class A { private void show() System.out.println("Hi"); } class B extends A int show() return 10; }``1A?.,MNB NO CODE WILL COMPILE & RUN SUCESSFULLY

System.out.println("class A"); return 0; } What’s Wrong Here00 class A { static int show() System.out.println("class A"); return 0; } class B extends A void show() System.out.println("class B"); sample.java:12: show() in B cannot override show() in A; overridden method is st atic void show() ^ 1 error