SlideShare a Scribd company logo
1 of 25
Java - Inheritance
What is Inheritance?
Inheritance is a mechanism in which one
class acquires the property of another class.
For example, a child inherits the traits of
his/her parents.
With inheritance, we can reuse the fields
and methods of the existing class. Hence,
inheritance facilitates Reusability and is an
important concept of OOPs.
• Types of Inheritance
• There are Various types of inheritance :
• Single Inheritance:
• In Single Inheritance one class extends another
class (one class only).
•
Single Inheritance
• In above diagram, Class B extends only Class A.
Class A is a super class and Class B is a Sub-class.
• Multiple Inheritance:
• In Multiple Inheritance, one class extending more
than one class. Java does not support multiple
inheritance.
• Multiple Inheritance
• As per above diagram, Class C extends Class A
and Class B both.
• Multilevel Inheritance:
• In Multilevel Inheritance, one class can inherit
from a derived class. Hence, the derived class
becomes the base class for the new class.
• Multilevel Inheritance
• As per shown in diagram Class C is subclass of
B and B is a of subclass Class A.
• Hierarchical Inheritance:
• In Hierarchical Inheritance, one class is
inherited by many sub classes.
• Hierarchical Inheritance As per above
example, Class B, C, and D inherit the same
class A.
• Hybrid Inheritance:
• Hybrid inheritance is a combination of Single
and Multiple inheritance.
• As per above example, all the public and
protected members of Class A are inherited
into Class D, first via Class B and secondly via
Class C.
• Note: Java doesn't support hybrid/Multiple
inheritence
• JAVA INHERITANCE is a mechanism in which one
class acquires the property of another class.
• In Java, when an "Is-A" relationship exists
between two classes, we use Inheritance.
• super class
• Subclass
• Extends
• Why use inheritance in java
• For Method Overriding
• (so runtime polymorphism can be achieved).
• For Code Reusability.
• The syntax of Java Inheritance
• class Subclass-name extends Superclass-name
• {
• //methods and fields
• }
• The extends keyword indicates that you are
making a new class that derives from an existing
class. The meaning of "extends" is to increase the
functionality.
• class Employee{
• double salary=40000;
• }
• class Programmer extends Employee{
• int bonus=10000;
• }
•
• class Main{
• public static void main(String args[]){
• Programmer p=new Programmer();
• System.out.println("Programmer salary is:"+p.salary);
• System.out.println("Bonus of Programmer is:"+p.bonus);
• }
• }
• https://compiler.javatpoint.com/opr/online-java-compiler.jsp
IS-A relationship :
Single inheritance
• class Animal{
• void eat(){System.out.println("eating...");}
• }
• class Dog extends Animal{
• void bark(){System.out.println("barking...");}
• }
• class Main{
• public static void main(String args[]){
• Dog d=new Dog();
• d.bark();
• d.eat();
• }}
IS-A relationship :
Multilevel Inheritance
• class Animal{
• void eat(){System.out.println("eating...");}
• }
• class Dog extends Animal{
• void bark(){System.out.println("barking...");}
• }
• class BabyDog extends Dog{
• void weep(){System.out.println("weeping...");}
• }
• class Main{
• public static void main(String args[]){
• BabyDog d=new BabyDog();
• d.weep();
• d.bark();
• d.eat();
• }}
Has-A inheritance
• class Operation{
• int square(int n){
• return n*n;
• }
• }
•
• class Circle{
• Operation op;//aggregation
• double pi=3.14;
•
• double area(int radius){
• op=new Operation();
• int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).
• return pi*rsquare;
• }
•
•
•
• public static void main(String args[]){
• Circle c=new Circle();
• double result=c.area(5);
• System.out.println(result);
• }
• }
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.
Rules for Java Method Overriding
• The method must have the same name as in
the parent class
• The method must have the same parameter
as in the parent class.
• There must be an IS-A relationship
(inheritance).
• class Vehicle{
• //defining a method
• void run(){System.out.println("Vehicle is running");}
• }
• //Creating a child class
• class Bike2 extends Vehicle{
• //defining the same method as in the parent class
• void run(){System.out.println("Bike is running safely");}
•
• public static void main(String args[]){
• Bike2 obj = new Bike2();//creating object
• obj.run();//calling method
• }
• }
Super Keyword in Java
• The super keyword in Java is a reference variable which is used to
refer immediate parent class object.
• Whenever you create the instance of subclass, an instance of
parent class is created implicitly which is referred by super
reference variable.
• Usage of Java super Keyword
• super can be used to refer immediate parent class instance variable.
• super can be used to invoke immediate parent class method.
• super() can be used to invoke immediate parent class constructor.
•
super is used to refer immediate
parent class instance variable.
• class Animal{
• String color="white";
• }
• class Dog extends Animal{
• String color="black";
• void printColor(){
• System.out.println(color);//prints color of Dog class
• System.out.println(super.color);//prints color of Animal class
• }
• }
• class TestSuper1{
• public static void main(String args[]){
• Dog d=new Dog();
• d.printColor();
• }}
super can be used to invoke parent
class method
• class Animal{
• void eat(){System.out.println("eating...");}
• }
• class Dog extends Animal{
• void eat(){System.out.println("eating bread...");}
• void bark(){System.out.println("barking...");}
• void work(){
• super.eat();
• bark();
• }
• }
• class TestSuper2{
• public static void main(String args[]){
• Dog d=new Dog();
• d.work();
• }}
super is used to invoke parent class
constructor
• class Animal{
• Animal(){System.out.println("animal is created");}
• }
• class Dog extends Animal{
• Dog(){
• super();
• System.out.println("dog is created");
• }
• }
• class TestSuper3{
• public static void main(String args[]){
• Dog d=new Dog();
• }}
Final Keyword In Java
• The final keyword in java is used to restrict
the user. The java final keyword can be used in
many context. Final can be:
• variable
• method
• class
Java Runtime Polymorphism
• class Bike{
• void run(){System.out.println("running");}
• }
• class Splendor extends Bike{
• void run(){System.out.println("running safely with 60km");
}
•
• public static void main(String args[]){
• Bike b = new Splendor();//upcasting
• b.run();
• }
• }
• class Shape{
• void draw(){System.out.println("drawing...");}
• }
• class Rectangle extends Shape{
• void draw(){System.out.println("drawing rectangle...");}
• }
• class Circle extends Shape{
• void draw(){System.out.println("drawing circle...");}
• }
• class Triangle extends Shape{
• void draw(){System.out.println("drawing triangle...");}
• }
• class TestPolymorphism2{
• public static void main(String args[]){
• Shape s;
• s=new Rectangle();
• s.draw();
• s=new Circle();
• s.draw();
• s=new Triangle();
• s.draw();
• }
• }
Thanks

More Related Content

Similar to Java - Inheritance_multiple_inheritance.pptx

Similar to Java - Inheritance_multiple_inheritance.pptx (20)

Inheritance
InheritanceInheritance
Inheritance
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
29c
29c29c
29c
 
29csharp
29csharp29csharp
29csharp
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Inheritance in oop
Inheritance in oopInheritance in oop
Inheritance in oop
 
Inheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdfInheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdf
 
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
inheritanceinheritance
inheritance
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Inheritance1
Inheritance1Inheritance1
Inheritance1
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
 

Recently uploaded

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
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
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
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
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Recently uploaded (20)

OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
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
 
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
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
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
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 

Java - Inheritance_multiple_inheritance.pptx

  • 2. What is Inheritance? Inheritance is a mechanism in which one class acquires the property of another class. For example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class. Hence, inheritance facilitates Reusability and is an important concept of OOPs.
  • 3. • Types of Inheritance • There are Various types of inheritance : • Single Inheritance: • In Single Inheritance one class extends another class (one class only). • Single Inheritance • In above diagram, Class B extends only Class A. Class A is a super class and Class B is a Sub-class.
  • 4. • Multiple Inheritance: • In Multiple Inheritance, one class extending more than one class. Java does not support multiple inheritance. • Multiple Inheritance • As per above diagram, Class C extends Class A and Class B both.
  • 5. • Multilevel Inheritance: • In Multilevel Inheritance, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class. • Multilevel Inheritance • As per shown in diagram Class C is subclass of B and B is a of subclass Class A.
  • 6. • Hierarchical Inheritance: • In Hierarchical Inheritance, one class is inherited by many sub classes. • Hierarchical Inheritance As per above example, Class B, C, and D inherit the same class A.
  • 7. • Hybrid Inheritance: • Hybrid inheritance is a combination of Single and Multiple inheritance. • As per above example, all the public and protected members of Class A are inherited into Class D, first via Class B and secondly via Class C. • Note: Java doesn't support hybrid/Multiple inheritence
  • 8. • JAVA INHERITANCE is a mechanism in which one class acquires the property of another class. • In Java, when an "Is-A" relationship exists between two classes, we use Inheritance. • super class • Subclass • Extends
  • 9. • Why use inheritance in java • For Method Overriding • (so runtime polymorphism can be achieved). • For Code Reusability.
  • 10. • The syntax of Java Inheritance • class Subclass-name extends Superclass-name • { • //methods and fields • } • The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.
  • 11. • class Employee{ • double salary=40000; • } • class Programmer extends Employee{ • int bonus=10000; • } • • class Main{ • public static void main(String args[]){ • Programmer p=new Programmer(); • System.out.println("Programmer salary is:"+p.salary); • System.out.println("Bonus of Programmer is:"+p.bonus); • } • } • https://compiler.javatpoint.com/opr/online-java-compiler.jsp
  • 12. IS-A relationship : Single inheritance • class Animal{ • void eat(){System.out.println("eating...");} • } • class Dog extends Animal{ • void bark(){System.out.println("barking...");} • } • class Main{ • public static void main(String args[]){ • Dog d=new Dog(); • d.bark(); • d.eat(); • }}
  • 13. IS-A relationship : Multilevel Inheritance • class Animal{ • void eat(){System.out.println("eating...");} • } • class Dog extends Animal{ • void bark(){System.out.println("barking...");} • } • class BabyDog extends Dog{ • void weep(){System.out.println("weeping...");} • } • class Main{ • public static void main(String args[]){ • BabyDog d=new BabyDog(); • d.weep(); • d.bark(); • d.eat(); • }}
  • 14. Has-A inheritance • class Operation{ • int square(int n){ • return n*n; • } • } • • class Circle{ • Operation op;//aggregation • double pi=3.14; • • double area(int radius){ • op=new Operation(); • int rsquare=op.square(radius);//code reusability (i.e. delegates the method call). • return pi*rsquare; • } • • • • public static void main(String args[]){ • Circle c=new Circle(); • double result=c.area(5); • System.out.println(result); • } • }
  • 15. 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.
  • 16. Rules for Java Method Overriding • The method must have the same name as in the parent class • The method must have the same parameter as in the parent class. • There must be an IS-A relationship (inheritance).
  • 17. • class Vehicle{ • //defining a method • void run(){System.out.println("Vehicle is running");} • } • //Creating a child class • class Bike2 extends Vehicle{ • //defining the same method as in the parent class • void run(){System.out.println("Bike is running safely");} • • public static void main(String args[]){ • Bike2 obj = new Bike2();//creating object • obj.run();//calling method • } • }
  • 18. Super Keyword in Java • The super keyword in Java is a reference variable which is used to refer immediate parent class object. • Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable. • Usage of Java super Keyword • super can be used to refer immediate parent class instance variable. • super can be used to invoke immediate parent class method. • super() can be used to invoke immediate parent class constructor. •
  • 19. super is used to refer immediate parent class instance variable. • class Animal{ • String color="white"; • } • class Dog extends Animal{ • String color="black"; • void printColor(){ • System.out.println(color);//prints color of Dog class • System.out.println(super.color);//prints color of Animal class • } • } • class TestSuper1{ • public static void main(String args[]){ • Dog d=new Dog(); • d.printColor(); • }}
  • 20. super can be used to invoke parent class method • class Animal{ • void eat(){System.out.println("eating...");} • } • class Dog extends Animal{ • void eat(){System.out.println("eating bread...");} • void bark(){System.out.println("barking...");} • void work(){ • super.eat(); • bark(); • } • } • class TestSuper2{ • public static void main(String args[]){ • Dog d=new Dog(); • d.work(); • }}
  • 21. super is used to invoke parent class constructor • class Animal{ • Animal(){System.out.println("animal is created");} • } • class Dog extends Animal{ • Dog(){ • super(); • System.out.println("dog is created"); • } • } • class TestSuper3{ • public static void main(String args[]){ • Dog d=new Dog(); • }}
  • 22. Final Keyword In Java • The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be: • variable • method • class
  • 23. Java Runtime Polymorphism • class Bike{ • void run(){System.out.println("running");} • } • class Splendor extends Bike{ • void run(){System.out.println("running safely with 60km"); } • • public static void main(String args[]){ • Bike b = new Splendor();//upcasting • b.run(); • } • }
  • 24. • class Shape{ • void draw(){System.out.println("drawing...");} • } • class Rectangle extends Shape{ • void draw(){System.out.println("drawing rectangle...");} • } • class Circle extends Shape{ • void draw(){System.out.println("drawing circle...");} • } • class Triangle extends Shape{ • void draw(){System.out.println("drawing triangle...");} • } • class TestPolymorphism2{ • public static void main(String args[]){ • Shape s; • s=new Rectangle(); • s.draw(); • s=new Circle(); • s.draw(); • s=new Triangle(); • s.draw(); • } • }