METHOD OVERRIDING in JAVA

Slides:



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

INHERITANCE BASICS Reusability is achieved by INHERITANCE
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 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Inheritance The Basics in Java. Definition  A class that is derived from another class is called a subclass (also a derived class, extended class, or.
Programming in Java CSCI-2220 Object Oriented Programming.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
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.
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.
Classes, Interfaces and Packages
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 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
BY:- TOPS Technologies
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance.
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Static data members Constructors and Destructors
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.
Inheritance and Polymorphism
INHERITANCE IN JAVA.
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
More inheritance, Abstract Classes and Interfaces
Interface.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Sampath Kumar S Assistant Professor, SECE
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Abstract Classes Page
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Method Overriding in Java
Inheritance Cse 3rd year.
Java Programming, Second Edition
Java Inheritance.
Sampath Kumar S Assistant Professor, SECE
Chapter 9 Carrano Chapter 10 Small Java
Overview of C++ Polymorphism
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

METHOD OVERRIDING in JAVA

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java. Usage: It is used to provide specific implementation of a method that is already provided by its super class. Method overriding is used for runtime polymorphism Rule: method must have same name as in the parent class method must have same parameter as in the parent class

Method overriding Method overriding is one of the way by which java achieve Run Time Polymorphism. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.

Sample code Output: Parent's show() Child's show() class Parent {     void show() { System.out.println("Parent's show()"); } }   // Inherited class class Child extends Parent     // This method overrides show() of Parent     @Override     void show() { System.out.println("Child's show()"); } // Driver class class Main     public static void main(String[] args)     {         // If a Parent type reference refers to a Parent object, then Parent's show is called         Parent obj1 = new Parent();         obj1.show();          // If a Parent type reference refers to a Child object Child's show() is called. This is called RUN TIME         // POLYMORPHISM.         Parent obj2 = new Child();         obj2.show();     } Output: Parent's show() Child's show()

Rules for method overriding: Overriding and Access-Modifiers : // A Simple Java program to demonstrate // Overriding and Access-Modifiers   class Parent {     // private methods are not overridden private void m1() { System.out.println("From parent m1()");}  protected void m2() { System.out.println("From parent m2()"); } } class Child extends Parent     // new m1() method     // unique to Child class     private void m1() { System.out.println("From child m1()");}           // overriding method with more accessibility     public void m2() { System.out.println("From child m2()");} class Main {     public static void main(String[] args)     {         Parent obj1 = new Parent();         obj1.m2();         Parent obj2 = new Child();         obj2.m2();     } } Output : From parent m2() From child m2()  

Final methods can not be overridden If we don’t want a method to be overridden, we declare it as final. Please see Using final with Inheritance . class Parent {     // Can't be overridden     final void show() {  } }   class Child extends Parent     // This would produce error     void show() {  } Output : 13: error: show() in Child cannot override show() in Parent void show() { } ^ overridden method is final

Abstraction in Java Abstraction is a process of hiding the implementation details and showing only functionality to the user. A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body). It needs to be extended and its method implemented. It cannot be instantiated.

Some key points An abstract class must be declared with an abstract keyword. It can have abstract and non-abstract methods. It cannot be instantiated. It can have constructors and static methods also. It can have final methods which will force the subclass not to change the body of the method.

Abstract Method in Java A method which is declared as abstract and does not have implementation is known as an abstract method. Example of abstract method abstract void show();//no method body 

Abstract Methods Abstract classes can’t be instantiated and they require subclasses to offer implementation for their abstract methods by overriding them and then the subclasses can be instantiated. We use abstract methods when we want to force the same name and signature pattern in all the subclasses and do not want to give them the opportunity to use their own naming patterns but at the same time give them the flexibility to code these methods with their own specific requirements.

Example program: Abstract class with abstract methods abstract class Animal { String name; String species; //constructor Animal(String n, String s) name=n; species=s; } void eat(String food ) System.out.println(species +” ”+ “likes to have ” +food); abstract void sound();

Contd… Output Asiatic Lion likes to have flesh Lions Roar!!! Now any animal that wants to be instantiated must override the sound() method, otherwise it will be impossible to create an instance of that class. class Lion extends Animal { Lion() super(“Lion”, “Asiatic Lion”); } void sound() System.out.println(“”Lions Roar!!!); public static void main(String [] args) Lion obj=new Lion(); obj.eat(“flesh”); obj.sound(); Output Asiatic Lion likes to have flesh Lions Roar!!!

points Abstract classes cannot be instantiated but they can have reference variables A class only can inherit only one abstract class . Abstract class may have abstract methods/ non-abstract methods It is mandatory for a subclass to override the abstract class methods for abstract class, otherwise the subclass also needs to be declared itself as abstract. Other non-abstract methods can also be overidden. Abstract classes may have constructors and variables like normal classes

For calling methods of the super class SUPER it refers to the parent class of a class in which the keyword is used. Used for: For calling methods of the super class For accessing the member variables of super class

SUPER: For calling methods of the super class class A { void show() System.out.println(“Super class method”); } Class B extends A super.show(); System.out.println(“Sub class method”); public static void main(String [] args) B s2=new B(); S2.show(); SUPER: For calling methods of the super class class A { void show() System.out.println(“Super class method”); } Class B extends A System.out.println(“Sub class method”); public static void main(String [] args) A s1=new A(); S1.show(); B s2=new B(); S2.show(); Output Super class method Sub class method Output Super class method Sub class method

SUPER: Accessing parent class variables class Super_variable { Int b=30; } class SubClass extends Super_variable int b=12; void show() System.out.println(“Subclass variable:”+b); System.out.println(“Super class variable:”+ super.b); public static void main(String[] args) SubClass s=new SubClass (); s.show(); Output; Subclass variable: 12 Super class variable: 30