SlideShare una empresa de Scribd logo
1 de 16
Inheritance DhrubojyotiKayal
Need for inheritance Inheritance Rules Syntax Method Overriding Inheritance and method call Protected Upcasting Agenda
You create a class Vehicle and then forced to create a new class Truck because the later has some behavior(methods) similar to Vehicle but some rather unique You want to re-use the code written in class Vehicle in other similar classes. Need for inheritance
Take the Vehicle class, clone it and create a new class Truck Keep the common behavior in clone Make additions and modifications in the clone That’s inheritance for you! Original class = base class Clone class = derived class It creates a new class as a type of an existing class.  You literally take the form of the existing class and add code to it without modifying the existing class.  Inheritance
When base class is changed derived class automatically gets those changes. Reverse is not true. Any number of classes can be derived from base class. Put re-usable or generic behavior in base class Put specific behavior in derived class Represents is-a relationship Derived class can add additional methods and fields. Base class fields are not inherited. Logical they don't define special behavior Private methods are not inherited Rules
You’re always doing inheritance when you create a class, because unless you explicitly inherit from some other class, you implicitly inherit from Java’s standard root class Object  extends  public class Rectangle extends Shape { } Syntax
Inheritance in Action public class Rectangle   private double length;   private double breadth;   extends Shape{   public void draw() { System.out.println(“Drawing a rectangle : length : ” + length + “ breadth : ” + breadth);   }   public void setDimensions (double len,doublebrd) { 	length = len; 	breadth = brd; } } public class Shape {   public void draw() { System.out.println(“Drawing a shape”);   } } Method Overriding
Changing the base class method in the derived class You need more specific behavior relevant to the derived class Derived class method must have exactly the same signature and return type as in the base class method @Override New in Java SE 5 Prevents accidental overload instead of override Method Overriding
Rectangle rectangle = new Rectangle() rectangle.draw()?  Base class draw? Derived class draw? “Lowest one wins” Lowest on inheritance tree Inheritance and Method call
Create a Java class ‘Animal’ with the following methods: makeNoise(); eat(); sleep(); roam(); Create a class ‘Canine’ inheriting from ‘Animal’ and overrides method roam(); Create a class ‘Wolf’ inheriting from ‘Canine’ and inheriting from ‘Canine’ and overrides methods – makeNoise(), eat() Each method return type is void and just prints the class name followed by the method name. Create a test class with a main method. In the main method create an instance of Wolf and invoke all the four methods in that order. Exercise (courtesy HFJava)
You want to keep the base class functionality and yet change the behavior in derived class So you need a mechanism to call the base class method Use super to invoke the immediate base class method  super.methodName(); Super
Change the method makeNoise() in Wolf class to invoke the makeNoise method in the parent class Exercise
This is private as far as the class user is concerned, but available to anyone who inherits from this class or anyone else in the same package. protected
Create a package – myshape Create a class Shape with one method draw which prints the class name and the method name Create a class MyShapeUser with a main method. Create an instance of Shape and invoke the draw method Create a new package yourshape Create a class YourShapeUser with a main method. Create an instance of Shape in myshape package and try to invoke the draw method Now create a class YourShape which extends Shape in myshape package. Create an instance of YourShape in YourShapeUser and try to invoke the draw method Exercise
The new class is a type of the existing class Java supports assigning derived class objects to base class reference Shape shape = new Rectangle(); The act of converting a derived (Rectangle) reference into a base (Instrument) reference is called upcasting. The term is derived from how inheritence tree is drawn Opposite is downcasting Upcasting
Q&A

Más contenido relacionado

La actualidad más candente

Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - InheritanceOum Saokosal
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructorsJan Niño Acierto
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance SlidesAhsan Raja
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javachauhankapil
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & PolymorphismSAGARDAVE29
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritanceshatha00
 
The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196Mahmoud Samir Fayed
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdmHarshal Misalkar
 
Mariia Havrylovych "Active learning and weak supervision in NLP projects"
Mariia Havrylovych "Active learning and weak supervision in NLP projects"Mariia Havrylovych "Active learning and weak supervision in NLP projects"
Mariia Havrylovych "Active learning and weak supervision in NLP projects"Fwdays
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 PolymorphismMahmoud Alfarra
 
Java Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassJava Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassOum Saokosal
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
The Ring programming language version 1.4 book - Part 20 of 30
The Ring programming language version 1.4 book - Part 20 of 30The Ring programming language version 1.4 book - Part 20 of 30
The Ring programming language version 1.4 book - Part 20 of 30Mahmoud Samir Fayed
 

La actualidad más candente (20)

Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & Polymorphism
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Java Methods
Java MethodsJava Methods
Java Methods
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Mariia Havrylovych "Active learning and weak supervision in NLP projects"
Mariia Havrylovych "Active learning and weak supervision in NLP projects"Mariia Havrylovych "Active learning and weak supervision in NLP projects"
Mariia Havrylovych "Active learning and weak supervision in NLP projects"
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassJava Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract Class
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
The Ring programming language version 1.4 book - Part 20 of 30
The Ring programming language version 1.4 book - Part 20 of 30The Ring programming language version 1.4 book - Part 20 of 30
The Ring programming language version 1.4 book - Part 20 of 30
 

Destacado

Top 10 Digital Signage Tips
Top 10 Digital Signage TipsTop 10 Digital Signage Tips
Top 10 Digital Signage Tipsdsignguy
 
Delta Region AIDS Education Training Center AR LPS
Delta Region AIDS Education Training Center AR LPSDelta Region AIDS Education Training Center AR LPS
Delta Region AIDS Education Training Center AR LPSdnewby700
 
Tue 121913after juses was born walter
Tue 121913after juses was born walterTue 121913after juses was born walter
Tue 121913after juses was born walterHeather Bian
 
Digital Signage for Public Spaces
Digital Signage for Public SpacesDigital Signage for Public Spaces
Digital Signage for Public Spacesdsignguy
 
Tue 040114 the call fanzhang
Tue 040114 the call fanzhangTue 040114 the call fanzhang
Tue 040114 the call fanzhangHeather Bian
 
Digital Signage for Auto Retailers
Digital Signage for Auto RetailersDigital Signage for Auto Retailers
Digital Signage for Auto Retailersdsignguy
 
Tue 031814 +-- faith zhaoliu
Tue 031814 +-- faith zhaoliuTue 031814 +-- faith zhaoliu
Tue 031814 +-- faith zhaoliuHeather Bian
 
Tue 01142014 chuangqi
Tue 01142014 chuangqiTue 01142014 chuangqi
Tue 01142014 chuangqiHeather Bian
 
Tue 042914 life of worship by helen yang
Tue 042914 life of worship by helen yangTue 042914 life of worship by helen yang
Tue 042914 life of worship by helen yangHeather Bian
 
Electrical Sales Manager Los Angeles
Electrical Sales Manager Los AngelesElectrical Sales Manager Los Angeles
Electrical Sales Manager Los Angelesganapathi krishna
 
Mumbai Attacks Presentation
Mumbai Attacks PresentationMumbai Attacks Presentation
Mumbai Attacks PresentationSarah Sienna
 
Digital Signage for Retail
Digital Signage for RetailDigital Signage for Retail
Digital Signage for Retaildsignguy
 
Mesure de la performance e-marketing
Mesure de la performance e-marketingMesure de la performance e-marketing
Mesure de la performance e-marketingohmyweb!
 

Destacado (20)

Top 10 Digital Signage Tips
Top 10 Digital Signage TipsTop 10 Digital Signage Tips
Top 10 Digital Signage Tips
 
Delta Region AIDS Education Training Center AR LPS
Delta Region AIDS Education Training Center AR LPSDelta Region AIDS Education Training Center AR LPS
Delta Region AIDS Education Training Center AR LPS
 
Tue 121913after juses was born walter
Tue 121913after juses was born walterTue 121913after juses was born walter
Tue 121913after juses was born walter
 
Digital Signage for Public Spaces
Digital Signage for Public SpacesDigital Signage for Public Spaces
Digital Signage for Public Spaces
 
Tue cheng chang
Tue cheng changTue cheng chang
Tue cheng chang
 
Tue 040114 the call fanzhang
Tue 040114 the call fanzhangTue 040114 the call fanzhang
Tue 040114 the call fanzhang
 
Digital Signage for Auto Retailers
Digital Signage for Auto RetailersDigital Signage for Auto Retailers
Digital Signage for Auto Retailers
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
Overview of ONEsite
Overview of ONEsiteOverview of ONEsite
Overview of ONEsite
 
Slide share ganapathi doc.
Slide share   ganapathi doc.Slide share   ganapathi doc.
Slide share ganapathi doc.
 
Tue 031814 +-- faith zhaoliu
Tue 031814 +-- faith zhaoliuTue 031814 +-- faith zhaoliu
Tue 031814 +-- faith zhaoliu
 
2 25-14
2 25-142 25-14
2 25-14
 
Tue 01142014 chuangqi
Tue 01142014 chuangqiTue 01142014 chuangqi
Tue 01142014 chuangqi
 
Tue 042914 life of worship by helen yang
Tue 042914 life of worship by helen yangTue 042914 life of worship by helen yang
Tue 042914 life of worship by helen yang
 
Electrical Sales Manager Los Angeles
Electrical Sales Manager Los AngelesElectrical Sales Manager Los Angeles
Electrical Sales Manager Los Angeles
 
Mumbai Attacks Presentation
Mumbai Attacks PresentationMumbai Attacks Presentation
Mumbai Attacks Presentation
 
Digital Signage for Retail
Digital Signage for RetailDigital Signage for Retail
Digital Signage for Retail
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Mesure de la performance e-marketing
Mesure de la performance e-marketingMesure de la performance e-marketing
Mesure de la performance e-marketing
 

Similar a 13 inheritance

06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.pptParikhitGhosh1
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1PRN USM
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceKuntal Bhowmick
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Threeamiable_indian
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3Narayana Swamy
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10Terry Yoast
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear ASHNA nadhm
 

Similar a 13 inheritance (20)

06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Reflection
ReflectionReflection
Reflection
 
Chap11
Chap11Chap11
Chap11
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3
 
Only oop
Only oopOnly oop
Only oop
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear
 
Inheritance
InheritanceInheritance
Inheritance
 

Más de dhrubo kayal

01 session tracking
01   session tracking01   session tracking
01 session trackingdhrubo kayal
 
03 handling requests
03 handling requests03 handling requests
03 handling requestsdhrubo kayal
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servletsdhrubo kayal
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setupdhrubo kayal
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanupdhrubo kayal
 
08 class and object
08   class and object08   class and object
08 class and objectdhrubo kayal
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variablesdhrubo kayal
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with javadhrubo kayal
 

Más de dhrubo kayal (20)

Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
19 reflection
19   reflection19   reflection
19 reflection
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
16 containers
16   containers16   containers
16 containers
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
12 encapsulation
12   encapsulation12   encapsulation
12 encapsulation
 
11 static
11   static11   static
11 static
 
10 access control
10   access control10   access control
10 access control
 
09 packages
09   packages09   packages
09 packages
 
08 class and object
08   class and object08   class and object
08 class and object
 
07 flow control
07   flow control07   flow control
07 flow control
 
05 operators
05   operators05   operators
05 operators
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 
02 what is java
02   what is java02   what is java
02 what is java
 

13 inheritance

  • 2. Need for inheritance Inheritance Rules Syntax Method Overriding Inheritance and method call Protected Upcasting Agenda
  • 3. You create a class Vehicle and then forced to create a new class Truck because the later has some behavior(methods) similar to Vehicle but some rather unique You want to re-use the code written in class Vehicle in other similar classes. Need for inheritance
  • 4. Take the Vehicle class, clone it and create a new class Truck Keep the common behavior in clone Make additions and modifications in the clone That’s inheritance for you! Original class = base class Clone class = derived class It creates a new class as a type of an existing class. You literally take the form of the existing class and add code to it without modifying the existing class. Inheritance
  • 5. When base class is changed derived class automatically gets those changes. Reverse is not true. Any number of classes can be derived from base class. Put re-usable or generic behavior in base class Put specific behavior in derived class Represents is-a relationship Derived class can add additional methods and fields. Base class fields are not inherited. Logical they don't define special behavior Private methods are not inherited Rules
  • 6. You’re always doing inheritance when you create a class, because unless you explicitly inherit from some other class, you implicitly inherit from Java’s standard root class Object extends public class Rectangle extends Shape { } Syntax
  • 7. Inheritance in Action public class Rectangle private double length; private double breadth; extends Shape{ public void draw() { System.out.println(“Drawing a rectangle : length : ” + length + “ breadth : ” + breadth); } public void setDimensions (double len,doublebrd) { length = len; breadth = brd; } } public class Shape { public void draw() { System.out.println(“Drawing a shape”); } } Method Overriding
  • 8. Changing the base class method in the derived class You need more specific behavior relevant to the derived class Derived class method must have exactly the same signature and return type as in the base class method @Override New in Java SE 5 Prevents accidental overload instead of override Method Overriding
  • 9. Rectangle rectangle = new Rectangle() rectangle.draw()? Base class draw? Derived class draw? “Lowest one wins” Lowest on inheritance tree Inheritance and Method call
  • 10. Create a Java class ‘Animal’ with the following methods: makeNoise(); eat(); sleep(); roam(); Create a class ‘Canine’ inheriting from ‘Animal’ and overrides method roam(); Create a class ‘Wolf’ inheriting from ‘Canine’ and inheriting from ‘Canine’ and overrides methods – makeNoise(), eat() Each method return type is void and just prints the class name followed by the method name. Create a test class with a main method. In the main method create an instance of Wolf and invoke all the four methods in that order. Exercise (courtesy HFJava)
  • 11. You want to keep the base class functionality and yet change the behavior in derived class So you need a mechanism to call the base class method Use super to invoke the immediate base class method super.methodName(); Super
  • 12. Change the method makeNoise() in Wolf class to invoke the makeNoise method in the parent class Exercise
  • 13. This is private as far as the class user is concerned, but available to anyone who inherits from this class or anyone else in the same package. protected
  • 14. Create a package – myshape Create a class Shape with one method draw which prints the class name and the method name Create a class MyShapeUser with a main method. Create an instance of Shape and invoke the draw method Create a new package yourshape Create a class YourShapeUser with a main method. Create an instance of Shape in myshape package and try to invoke the draw method Now create a class YourShape which extends Shape in myshape package. Create an instance of YourShape in YourShapeUser and try to invoke the draw method Exercise
  • 15. The new class is a type of the existing class Java supports assigning derived class objects to base class reference Shape shape = new Rectangle(); The act of converting a derived (Rectangle) reference into a base (Instrument) reference is called upcasting. The term is derived from how inheritence tree is drawn Opposite is downcasting Upcasting
  • 16. Q&A