Download presentation
Presentation is loading. Please wait.
1
METHOD OVERRIDING in JAVA
2
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
3
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.
4
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 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()
5
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()
6
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
7
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.
8
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.
9
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
10
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.
11
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();
12
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!!!
13
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
14
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
15
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
16
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.