SlideShare a Scribd company logo
1 of 14
CMP-2123-Object Oriented
Programming
Lecture 7
By
Abrar Ahmad
Abrar.ahmad14@ce.ceme.edu.pk
Java - Overriding
Badar Waseer arbabwaseergmail.com 2
• In the last lecture we talked about superclasses and subclasses. If a
class inherits a method from its superclass, then there is a chance to
override the method provided that it is not marked final
• The benefit of overriding is: ability to define a behavior that's specific
to the subclass type, which means a subclass can implement a parent
class method based on its requirement
• In object-oriented terms, overriding means to override the
functionality of an existing method
Badar Waseer arbabwaseergmail.com 3
Badar Waseer arbabwaseergmail.com 4
Badar Waseer arbabwaseergmail.com 5
• In the above example, you can see that even though b is a type of
Animal it runs the move method in the Dog class. The reason for this
is: In compile time, the check is made on the reference type.
However, in the runtime, JVM figures out the object type and would
run the method that belongs to that particular object.
• Therefore, in the above example, the program will compile properly
since Animal class has the method move. Then, at the runtime, it runs
the method specific for that object
Badar Waseer arbabwaseergmail.com 6
Badar Waseer arbabwaseergmail.com 7
This program will throw a compile time error since b's
reference type Animal doesn't have a method by the name
of bark.
Badar Waseer arbabwaseergmail.com 8
Rules for Method Overriding
• The argument list should be exactly the same as that of the
overridden method.
• The return type should be the same or a subtype of the return type
declared in the original overridden method in the superclass.
• The access level cannot be more restrictive than the overridden
method's access level. For example: If the superclass method is
declared public then the overridding method in the sub class cannot
be either private or protected.
• Instance methods can be overridden only if they are inherited by the
subclass.
Badar Waseer arbabwaseergmail.com 9
• A method declared final cannot be overridden.
• A method declared static cannot be overridden but can be re-
declared.
• If a method cannot be inherited, then it cannot be overridden.
• A subclass within the same package as the instance's superclass can
override any superclass method that is not declared private or final.
• A subclass in a different package can only override the non-final
methods declared public or protected.
Badar Waseer arbabwaseergmail.com 10
• An overriding method can throw any uncheck exceptions, regardless
of whether the overridden method throws exceptions or not.
However, the overriding method should not throw checked
exceptions that are new or broader than the ones declared by the
overridden method. The overriding method can throw narrower or
fewer exceptions than the overridden method.
• Constructors cannot be overridden.
Badar Waseer arbabwaseergmail.com 11
Using the super Keyword
• When invoking a superclass version of an overridden method
the super keyword is used
Badar Waseer arbabwaseergmail.com 12
Badar Waseer arbabwaseergmail.com 13
Badar Waseer arbabwaseergmail.com 14

More Related Content

What's hot

This keyword in java
This keyword in javaThis keyword in java
This keyword in javaHitesh Kumar
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Bt0074, oops with java
Bt0074, oops with javaBt0074, oops with java
Bt0074, oops with javasmumbahelp
 
Chapter 05 polymorphism extra
Chapter 05 polymorphism extraChapter 05 polymorphism extra
Chapter 05 polymorphism extraNurhanna Aziz
 
Java component
Java componentJava component
Java componentkrishashi
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHPRick Ogden
 
Semantic DEX Components
Semantic DEX ComponentsSemantic DEX Components
Semantic DEX ComponentsDavid Price
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and AkkaYung-Lin Ho
 

What's hot (14)

This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Driver
DriverDriver
Driver
 
Bt0074, oops with java
Bt0074, oops with javaBt0074, oops with java
Bt0074, oops with java
 
Akka - A Brief Intro
Akka - A Brief IntroAkka - A Brief Intro
Akka - A Brief Intro
 
Chapter 05 polymorphism extra
Chapter 05 polymorphism extraChapter 05 polymorphism extra
Chapter 05 polymorphism extra
 
Chapter8
Chapter8Chapter8
Chapter8
 
Java component
Java componentJava component
Java component
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
 
Semantic DEX Components
Semantic DEX ComponentsSemantic DEX Components
Semantic DEX Components
 
Chapter 9 java
Chapter 9 javaChapter 9 java
Chapter 9 java
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and Akka
 
Akka framework
Akka frameworkAkka framework
Akka framework
 

Similar to Lec 1.7 Object Oriented Programming

5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.pptAshwathGupta
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and PolymorphismKartikKapgate
 
Unit 7 inheritance
Unit 7 inheritanceUnit 7 inheritance
Unit 7 inheritanceatcnerd
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphismTOPS Technologies
 
Chapter 03 enscapsulation
Chapter 03 enscapsulationChapter 03 enscapsulation
Chapter 03 enscapsulationNurhanna Aziz
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Nuzhat Memon
 
Chapter 7 & 8 Classes in Java and Functions.pdf
Chapter 7 & 8 Classes in Java and Functions.pdfChapter 7 & 8 Classes in Java and Functions.pdf
Chapter 7 & 8 Classes in Java and Functions.pdfKavitaHegde4
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingWondimuBantihun1
 

Similar to Lec 1.7 Object Oriented Programming (20)

Java session2
Java session2Java session2
Java session2
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Unit 7 inheritance
Unit 7 inheritanceUnit 7 inheritance
Unit 7 inheritance
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Unit 2
Unit 2Unit 2
Unit 2
 
javainheritance
javainheritancejavainheritance
javainheritance
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphism
 
Chapter 03 enscapsulation
Chapter 03 enscapsulationChapter 03 enscapsulation
Chapter 03 enscapsulation
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)
 
L5
L5L5
L5
 
Chapter 7 & 8 Classes in Java and Functions.pdf
Chapter 7 & 8 Classes in Java and Functions.pdfChapter 7 & 8 Classes in Java and Functions.pdf
Chapter 7 & 8 Classes in Java and Functions.pdf
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
 

More from Badar Waseer

Ch 03 s.e agile development
Ch 03 s.e agile developmentCh 03 s.e agile development
Ch 03 s.e agile developmentBadar Waseer
 
Ch 02 s.e software process models 1
Ch 02 s.e software process models   1Ch 02 s.e software process models   1
Ch 02 s.e software process models 1Badar Waseer
 
Ch 01 s.e introduction
Ch 01 s.e introductionCh 01 s.e introduction
Ch 01 s.e introductionBadar Waseer
 
Ch 14 s.e use case diagrams
Ch 14 s.e use case diagramsCh 14 s.e use case diagrams
Ch 14 s.e use case diagramsBadar Waseer
 
Lec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented ProgrammingLec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented ProgrammingBadar Waseer
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingBadar Waseer
 
Multimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skillsMultimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skillsBadar Waseer
 
Multimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animationMultimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animationBadar Waseer
 
Multimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 videoMultimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 videoBadar Waseer
 
Multimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 AudioMultimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 AudioBadar Waseer
 
Multimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 MultimediaMultimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 MultimediaBadar Waseer
 
Multimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 deliveringMultimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 deliveringBadar Waseer
 
Agriculture Information System (AIS)
Agriculture Information System (AIS)Agriculture Information System (AIS)
Agriculture Information System (AIS)Badar Waseer
 
Output Devices UoS Fsd
Output Devices UoS FsdOutput Devices UoS Fsd
Output Devices UoS FsdBadar Waseer
 

More from Badar Waseer (15)

Ch 03 s.e agile development
Ch 03 s.e agile developmentCh 03 s.e agile development
Ch 03 s.e agile development
 
Ch 02 s.e software process models 1
Ch 02 s.e software process models   1Ch 02 s.e software process models   1
Ch 02 s.e software process models 1
 
Ch 01 s.e introduction
Ch 01 s.e introductionCh 01 s.e introduction
Ch 01 s.e introduction
 
Ch 14 s.e use case diagrams
Ch 14 s.e use case diagramsCh 14 s.e use case diagrams
Ch 14 s.e use case diagrams
 
Ch 13 s.e cmmi
Ch 13 s.e cmmiCh 13 s.e cmmi
Ch 13 s.e cmmi
 
Lec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented ProgrammingLec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented Programming
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented Programming
 
Multimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skillsMultimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skills
 
Multimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animationMultimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animation
 
Multimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 videoMultimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 video
 
Multimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 AudioMultimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 Audio
 
Multimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 MultimediaMultimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 Multimedia
 
Multimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 deliveringMultimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 delivering
 
Agriculture Information System (AIS)
Agriculture Information System (AIS)Agriculture Information System (AIS)
Agriculture Information System (AIS)
 
Output Devices UoS Fsd
Output Devices UoS FsdOutput Devices UoS Fsd
Output Devices UoS Fsd
 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Recently uploaded (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 

Lec 1.7 Object Oriented Programming

  • 1. CMP-2123-Object Oriented Programming Lecture 7 By Abrar Ahmad Abrar.ahmad14@ce.ceme.edu.pk
  • 2. Java - Overriding Badar Waseer arbabwaseergmail.com 2
  • 3. • In the last lecture we talked about superclasses and subclasses. If a class inherits a method from its superclass, then there is a chance to override the method provided that it is not marked final • The benefit of overriding is: ability to define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement • In object-oriented terms, overriding means to override the functionality of an existing method Badar Waseer arbabwaseergmail.com 3
  • 6. • In the above example, you can see that even though b is a type of Animal it runs the move method in the Dog class. The reason for this is: In compile time, the check is made on the reference type. However, in the runtime, JVM figures out the object type and would run the method that belongs to that particular object. • Therefore, in the above example, the program will compile properly since Animal class has the method move. Then, at the runtime, it runs the method specific for that object Badar Waseer arbabwaseergmail.com 6
  • 8. This program will throw a compile time error since b's reference type Animal doesn't have a method by the name of bark. Badar Waseer arbabwaseergmail.com 8
  • 9. Rules for Method Overriding • The argument list should be exactly the same as that of the overridden method. • The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass. • The access level cannot be more restrictive than the overridden method's access level. For example: If the superclass method is declared public then the overridding method in the sub class cannot be either private or protected. • Instance methods can be overridden only if they are inherited by the subclass. Badar Waseer arbabwaseergmail.com 9
  • 10. • A method declared final cannot be overridden. • A method declared static cannot be overridden but can be re- declared. • If a method cannot be inherited, then it cannot be overridden. • A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. • A subclass in a different package can only override the non-final methods declared public or protected. Badar Waseer arbabwaseergmail.com 10
  • 11. • An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method. • Constructors cannot be overridden. Badar Waseer arbabwaseergmail.com 11
  • 12. Using the super Keyword • When invoking a superclass version of an overridden method the super keyword is used Badar Waseer arbabwaseergmail.com 12