Method Overriding in Java

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Bayu Priyambadha, S.Kom. * Poly = Many, Morph = form * Polymorphism refers to a principle in biology in which an organism or species can have many different.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Peyman Dodangeh Sharif University of Technology Fall 2014.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Methods and Classes. Method Overloading Two or more methods within the same class that share the same name but different parameters class OverloadDemo.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
BY:- TOPS Technologies
OOPM. Java Array Array is a collection of similar type of elements that have contiguous memory location. Java array is an object the contains elements.
Modern Programming Tools And Techniques-I
Polymorphism.
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Advanced Programming in Java
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
Lecture 17: Polymorphism (Part II)
INHERITANCE IN JAVA.
ATS Application Programming: Java Programming
Programming in Java Text Books :
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
Inheritance Visit for more Learning Resources.
Interface.
Sampath Kumar S Assistant Professor, SECE
Object-Oriented Programming
Sampath Kumar S Assistant Professor, SECE
METHOD OVERRIDING in JAVA
S.VIGNESH Assistant Professor/CSE, SECE
Sampath Kumar.S Assistant Professor/IT, SECE
S.VIGNESH Assistant Professor, SECE
Method Overloading in JAVA
Sampath Kumar S Assistant Professor, SECE
Inheritance Inheritance is a fundamental Object Oriented concept
Sampath Kumar S Assistant Professor, SECE
Graphics Programming - Frames
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Lecture 18: Polymorphism (Part II)
Inheritance and Polymorphism
Sampath Kumar S Assistant Professor, SECE
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Eighth step for Learning C++ Programming
C++ Programming CLASS This pointer Static Class Friend Class
Sampath Kumar S Assistant Professor, SECE
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Advanced Programming in Java
CPSC 233 Tutorial 13 March 11/12th, 2015.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Inheritance and Polymorphism
Presentation transcript:

Method Overriding in Java Sampath Kumar S Assistant Professor, SECE

Method Overriding in Java 1/12/2019 If subclass (child class) has the same method as declared in the parent class, it is known as method overriding. In other words, If subclass provides the specific (different) implementation of the method that has been provided by one of its parent class, it is known as Method Overriding. Sampath Kumar S, AP

Advantages: 1/12/2019 Method Overriding is used to provide specific implementation of a method that is already provided by its super class. Method Overriding is used for Runtime Polymorphism Sampath Kumar S, AP

Rules for Method Overriding 1/12/2019 Method must have same name as in the parent class Method must have same parameter as in the parent class. Must be IS-A relationship (inheritance). Sampath Kumar S, AP

Example Output: Dogs can run 1/12/2019 class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ System.out.println("Dogs can run"); public class TestDog{ public static void main(String args[]){ Dog d = new Dog(); d.move(); //Runs the method in Dog class } } Output: Dogs can run Sampath Kumar S, AP

1/12/2019 Another Example Sampath Kumar S, AP

Another Example Output: 1/12/2019 Another Example class Bank { int getRateOfInterest(){ return 0; } } class SBI extends Bank { return 8; } } class ICICI extends Bank { return 7; } } class AXIS extends Bank{ return 9; class Test{ public static void main(String args[]){ SBI s=new SBI(); ICICI i=new ICICI(); AXIS a=new AXIS(); System.out.println("SBI Rate of Interest: "+s.getRateOfInterest()); System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest()); System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest()); } } Output: SBI Rate of Interest: 8 ICICI Rate of Interest: 7 AXIS Rate of Interest: 9 Sampath Kumar S, AP

Difference between method Overloading and Overriding? 1/12/2019 Difference between method Overloading and Overriding? There are three basic differences between the method overloading and method overriding. They are as follows: Method Overloading Method Overriding 1) Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class. 2) Method overloading is performed within a class. Method overriding occurs in two classes that have IS-A relationship. 3) In case of method overloading parameter must be different. In case of method overriding parameter must be same. Sampath Kumar S, AP

1/12/2019 Sampath Kumar S, AP

1/12/2019 Thank You  Sampath Kumar S, AP