SlideShare una empresa de Scribd logo
1 de 19
Method Overriding
Method Overriding
 If a method of the subclass has the same
signature(name and parameter list) and return type
as that of the method in super class then we say that
the methods are overrided.
 The process is known as “Method Overriding”
 The ability of a subclass to override a method allows
a class to modify behavior as needed.
 It is also a method of implementing
“polymorphism”
Example
public Class OverrideSuper
{
void test( )
{
System.out.println(“No parameters”);
}
void test(int a)
{
System.out.println(“a: ”+a);
}
}
Example cont..
public Class OverrideSub extends OverrideSuper
{
void test( )
{
int x,y,z;
x=10;
y=20;
z=x+y;
System.out.println(z);
}
void test(int a)
{
a=a+10;
System.out.println(“a: ”+a);
}
}
Example Cont..
public class OverrideDriver
{
public static void main(String args[])
{
OverrideSuper o=new OverrideSuper( );
o.test( );
o.test(10);
OverrideSub s=new OverrideSub( );
s.test( );
s.test(10);
}
}
Accessing Super class Members
 If your method overrides one of its superclass's
methods, you can invoke the overridden method
through the use of the keyword super.
Example
public Class OverrideSuper
{
void test( )
{
System.out.println(“No parameters”);
}
void test(int a)
{
System.out.println(“a: ”+a);
}
}
Example cont..
public Class OverrideSub extends OverrideSuper
{
void test( )
{
super.test();
int x,y,z;
x=10;
y=20;
z=x+y;
System.out.println(z);
}
void test(int a)
{
super.test(20);
a=a+10;
System.out.println(“a: ”+a);
}
}
Example cont..
public class OverrideDriver
{
public static void main(String args[])
{
OverrideSuper o=new OverrideSuper( );
o.test( );
o.test(10);
OverrideSub s=new OverrideSub( );
s.test( );
s.test(10);
}
}
Accessing Super class Data Members
 Like methods we can have data members in the
subclass having the same name as that of the super
class.
 Use super keyword to access the data members as
well.
 Example: super.datamember_name
Example
public Class OverrideSuper
{
int b;
void test( )
{
System.out.println(“No parameters”);
}
void test(int a)
{
System.out.println(“a: ”+a);
}
}
Example cont..
public Class OverrideSub extends OverrideSuper
{
int b;
void test( )
{
super.test();
int x,y,z;
x=10;
y=20;
z=x+y;
System.out.println(z);
}
void test(int a)
{
super.test(20);
super.b=30;
a=a+10;
System.out.println(“a: ”+a);
System.out.println(“b: ”+super.b);
}
}
Example cont..
public class OverrideDriver
{
public static void main(String args[])
{
OverrideSuper o=new OverrideSuper( );
o.test( );
o.test(10);
OverrideSub s=new OverrideSub( );
s.test( );
s.test(10);
}
}
Lab Task
 Use the following class hierarchy:
Shape
Circle Square Triangle
• All of the classes contain a method known as printValues.
• The printValues method of the sub class should call the method of the super
class.
• Note: Use the classes that you have coded in the previous lab.
Calling constructor of the super class
 The syntax for calling a superclass constructor is
 super();
 super(parameter list);
 With super(), the superclass no-argument
constructor is called. With super(parameter list),
the superclass constructor with a matching
parameter list is called.
 Note: Invocation of a superclass constructor
must be the first line in the subclass
constructor.
Example
public Class OverrideSuper
{
int b;
OverrideSuper( )
{
System.out.println(“super class”);
}
OverrideSuper(int a)
{
b=a;
System.out.println(“b: ”+b);
System.out.println(“super class”);
}
}
Example cont..
public Class OverrideSub extends OverrideSuper
{
int c;
OverrideSub( )
{
super();
c=20;
System.out.println(“sub class”);
}
OverrideSub (int a)
{
super(20);
c=a;
System.out.println(“sub class”);
}
}
Example cont..
public class OverrideDriver
{
public static void main(String args[])
{
OverrideSuper o=new OverrideSuper( );
OverrideSuper o=new OverrideSuper(30 );
OverrideSub s=new OverrideSub( );
OverrideSub s=new OverrideSub(40 );
}
}
Lab Task 2
 Add call to the super class constructor in all the sub
class constructors.

Más contenido relacionado

La actualidad más candente

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 

La actualidad más candente (20)

Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Exception handling
Exception handlingException handling
Exception handling
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 

Destacado

Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Prasad Sawant
 

Destacado (16)

Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
 
Method overriding in java
Method overriding in javaMethod overriding in java
Method overriding in java
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overriding
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 

Similar a Method overriding

Practice programs
Practice programsPractice programs
Practice programs
Abbott
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 

Similar a Method overriding (20)

Method overloading
Method overloadingMethod overloading
Method overloading
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Practice programs
Practice programsPractice programs
Practice programs
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Inheritance
InheritanceInheritance
Inheritance
 
Methods Of Thread Class
Methods Of Thread ClassMethods Of Thread Class
Methods Of Thread Class
 
Java programs
Java programsJava programs
Java programs
 
Inheritance
InheritanceInheritance
Inheritance
 
Java programs
Java programsJava programs
Java programs
 
Android taipei 20160225 淺談closure
Android taipei 20160225   淺談closureAndroid taipei 20160225   淺談closure
Android taipei 20160225 淺談closure
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contd
 
Core java Essentials
Core java EssentialsCore java Essentials
Core java Essentials
 

Último

Último (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 

Method overriding

  • 2. Method Overriding  If a method of the subclass has the same signature(name and parameter list) and return type as that of the method in super class then we say that the methods are overrided.  The process is known as “Method Overriding”  The ability of a subclass to override a method allows a class to modify behavior as needed.  It is also a method of implementing “polymorphism”
  • 3. Example public Class OverrideSuper { void test( ) { System.out.println(“No parameters”); } void test(int a) { System.out.println(“a: ”+a); } }
  • 4. Example cont.. public Class OverrideSub extends OverrideSuper { void test( ) { int x,y,z; x=10; y=20; z=x+y; System.out.println(z); } void test(int a) { a=a+10; System.out.println(“a: ”+a); } }
  • 5. Example Cont.. public class OverrideDriver { public static void main(String args[]) { OverrideSuper o=new OverrideSuper( ); o.test( ); o.test(10); OverrideSub s=new OverrideSub( ); s.test( ); s.test(10); } }
  • 6. Accessing Super class Members  If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.
  • 7. Example public Class OverrideSuper { void test( ) { System.out.println(“No parameters”); } void test(int a) { System.out.println(“a: ”+a); } }
  • 8. Example cont.. public Class OverrideSub extends OverrideSuper { void test( ) { super.test(); int x,y,z; x=10; y=20; z=x+y; System.out.println(z); } void test(int a) { super.test(20); a=a+10; System.out.println(“a: ”+a); } }
  • 9. Example cont.. public class OverrideDriver { public static void main(String args[]) { OverrideSuper o=new OverrideSuper( ); o.test( ); o.test(10); OverrideSub s=new OverrideSub( ); s.test( ); s.test(10); } }
  • 10. Accessing Super class Data Members  Like methods we can have data members in the subclass having the same name as that of the super class.  Use super keyword to access the data members as well.  Example: super.datamember_name
  • 11. Example public Class OverrideSuper { int b; void test( ) { System.out.println(“No parameters”); } void test(int a) { System.out.println(“a: ”+a); } }
  • 12. Example cont.. public Class OverrideSub extends OverrideSuper { int b; void test( ) { super.test(); int x,y,z; x=10; y=20; z=x+y; System.out.println(z); } void test(int a) { super.test(20); super.b=30; a=a+10; System.out.println(“a: ”+a); System.out.println(“b: ”+super.b); } }
  • 13. Example cont.. public class OverrideDriver { public static void main(String args[]) { OverrideSuper o=new OverrideSuper( ); o.test( ); o.test(10); OverrideSub s=new OverrideSub( ); s.test( ); s.test(10); } }
  • 14. Lab Task  Use the following class hierarchy: Shape Circle Square Triangle • All of the classes contain a method known as printValues. • The printValues method of the sub class should call the method of the super class. • Note: Use the classes that you have coded in the previous lab.
  • 15. Calling constructor of the super class  The syntax for calling a superclass constructor is  super();  super(parameter list);  With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.  Note: Invocation of a superclass constructor must be the first line in the subclass constructor.
  • 16. Example public Class OverrideSuper { int b; OverrideSuper( ) { System.out.println(“super class”); } OverrideSuper(int a) { b=a; System.out.println(“b: ”+b); System.out.println(“super class”); } }
  • 17. Example cont.. public Class OverrideSub extends OverrideSuper { int c; OverrideSub( ) { super(); c=20; System.out.println(“sub class”); } OverrideSub (int a) { super(20); c=a; System.out.println(“sub class”); } }
  • 18. Example cont.. public class OverrideDriver { public static void main(String args[]) { OverrideSuper o=new OverrideSuper( ); OverrideSuper o=new OverrideSuper(30 ); OverrideSub s=new OverrideSub( ); OverrideSub s=new OverrideSub(40 ); } }
  • 19. Lab Task 2  Add call to the super class constructor in all the sub class constructors.