SlideShare a Scribd company logo
1 of 24
Basics of Inheritance




                        http://improvejava.blogspot.in/
                                                          1
Objectives

On completion of this period, you would be able to
learn

• Inheritance
• Inheritance basics
• Visibility modifiers




                     http://improvejava.blogspot.in/
Recap

In the previous lesson, you have learnt
• Command-line arguments




                  http://improvejava.blogspot.in/
Inheritance

• Inheritance is process by which one class gets the

properties from an existing class
• Object-oriented programming allows classes to inherit

   • Commonly used state and behavior from               other
     classes
• Inheritance is one of the principles of object oriented

programming
• Benefit of inheritance is the software reuse

                       http://improvejava.blogspot.in/
Inheritance Basics
• Inheritance allows a software developer to
  derive a new class from an existing one
• The existing class is called the parent class or
  superclass
• The derived class is called the child class or
  subclass
• Creates an is-a relationship
  The subclass is a more
    specific version of the
    original

                    http://improvejava.blogspot.in/
                                                      5
Inheritance Basics   contd..

   • In Java a class that is inheriting is called a super class

   • A class that is inherited is called sub class

   • Sub class is a specialized version of super class

   • Sub class inherits all instance variables and methods

   defined by super class and can have its own elements
   •   In Java one direct superclass, and each superclass has
   the potential for an unlimited number of subclasses


http://improvejava.blogspot.in/                                   6
Inheritance Basics                       contd..

                                                         Book
• In Fig. 22.1 Book is the existing class
• Dictionary and Novel are
                                        Dictionary Novel
     sub classes of Book
• Mystery and Romance are
     sub classes of Novel                      Mystery Romance

                                                    Fig 22.1 Book inheritance




                      http://improvejava.blogspot.in/
                                                                                7
Discussion
• What are the super and
 sub classes in Fig. 22.2
• Super class     Bicycle
• Sub classes
      MountBike
      RoadBike
      TandemBike
                                                Fig. 22.2 Bicycle example




                       http://improvejava.blogspot.in/
Discussion                           contd..
                                                             Object



                                         Class A                      Class D


                                                                      Class E
                           Class B                         Class C

                                        Fig. 22.3 Another example


• What are the super and sub classes in Fig. 22.3

• Super classes                      Object, Calls A, Class D
• Sub classes     Class A, Class B, Calss C, Class C, Class D
                         http://improvejava.blogspot.in/
Syntax
• To inherit a class from another class use keyword


                        “extends”
Syntax


class subclass-name extends superclass-name {
              ( body of the class )
}
If a class B is inherited from class A
    class B extends A               // A inherits B
                                    // Super class –A, Subclass- B
                        http://improvejava.blogspot.in/
Syntax contd...

• Consider the above Bicycle example

• Using the above syntax the inheritance can be shown as
 below

 class MountainBike extends Bicycle {
 // new fields and methods defining a mountain bike would go here
  }




                           http://improvejava.blogspot.in/
Example

class A {
    int i,j;
    void showij() {
           System.out.println(“ i and j :” + I + “ “ + j);
    }
}




                          http://improvejava.blogspot.in/
Example contd..

class B extends A {// creating subclass B from super class A
    int k;
    void showk() {
             System.out.println(“ k:” +k);
    }
    void sum() {
         System.out.println(“ i + j + k:” ( i+j+k));
    }
}
                          http://improvejava.blogspot.in/
Example                  contd..

class SimpleInheritance {

    public static void main( String args[]) {
           A superOb = new A();
           B subOb = new B();
           superOb.i = 10;// super class may be used by itself
           superOb.j = 20;
           System.out.println( “ Contents of superOb:”);
           superOb.showij();
           System.out.println();
                      http://improvejava.blogspot.in/
Example                          contd..
        subOb.i = 7; // sub class has access to all public members
        subOb.j = 8; // of its super class
        subOj.k = 9;
        System.out.println(“ Contents of subOj:” );
        subOb.showij();
        subOj.showk();
        System.out.println();
        System.out.println( “ sum of i,j and k in subOb:”);
        subOb.sum();
    }
}
                 http://improvejava.blogspot.in/
Output

Contents of superOb :
    i and j : 10 20
Contents of subOb :
     i and j : 7 8
      k:9
sum of I,j and k in subOb :
     i + j + k: 24




                      http://improvejava.blogspot.in/
Some Inheritance Details

• An instance of a child class does not rely on an instance
  of a parent class
  • Hence we could create a Dictionary object without
    having to create a Book object first

• Inheritance is a one-way street
  • The Book class cannot use variables or methods
    declared explicitly in the Dictionary class




                      http://improvejava.blogspot.in/
                                                              17
Summary
• In this class, we have discussed
   • Inheritance
   • Inheritance principle
   • Inheritance syntax
   • Super class
   • Sub class
   • Public, private and protected modifiers



                    http://improvejava.blogspot.in/
Quiz

1. A class which is inherited from other class is called
a) Super class
b) Sub class
c) Anonymous class
d) Object




                          http://improvejava.blogspot.in/
Quiz              contd...

2. A class which used to inherits a class is called
a)    Sub class
b)    Super class
c)    Anonymous class
d)    object




                  http://improvejava.blogspot.in/
Quiz               contd...

3. What is the keyword used to inherit a class

   a) create

   b) new

   c) this

   d) extends




                        http://improvejava.blogspot.in/
Quiz       contd...

4.Public variables volatile the principle of
a) Polymorphism
b) Encapsulation
c) Inheritance
d) Overloading




                                               22
Quiz Contd..

5. Variables and methods declared with following
   are inherited
a) private
b) public
c) protected
d) abstract




                   http://improvejava.blogspot.in/
Frequently Asked Questions
• What is inheritance ?
• Mention the key word to inherit a class from others
• What is super class ?
•   What is sub class ?
•   Write about public modifier
•   Write about private modifier
•   What protected modifier
•   Write a program in Java using modifiers
• Write a program in Java for inheriting a class from others

                          http://improvejava.blogspot.in/

More Related Content

Viewers also liked

Text field and textarea
Text field and textareaText field and textarea
Text field and textareamyrajendra
 
Creating a windowed program
Creating a windowed programCreating a windowed program
Creating a windowed programmyrajendra
 
Fundamentals of windows.64
Fundamentals of windows.64Fundamentals of windows.64
Fundamentals of windows.64myrajendra
 
Creating a frame within an applet
Creating a frame within an appletCreating a frame within an applet
Creating a frame within an appletmyrajendra
 
Checkbox and checkbox group
Checkbox and checkbox groupCheckbox and checkbox group
Checkbox and checkbox groupmyrajendra
 
Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttonsmyrajendra
 
Working with color and font
Working with color and fontWorking with color and font
Working with color and fontmyrajendra
 
Working with frames
Working with framesWorking with frames
Working with framesmyrajendra
 
‘ Final ‘ to avoid overriding
‘ Final ‘  to avoid overriding‘ Final ‘  to avoid overriding
‘ Final ‘ to avoid overridingmyrajendra
 
Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menusmyrajendra
 
This pointer .17
This pointer .17This pointer .17
This pointer .17myrajendra
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20myrajendra
 
Command line arguments.21
Command line arguments.21Command line arguments.21
Command line arguments.21myrajendra
 
Event handling63
Event handling63Event handling63
Event handling63myrajendra
 

Viewers also liked (14)

Text field and textarea
Text field and textareaText field and textarea
Text field and textarea
 
Creating a windowed program
Creating a windowed programCreating a windowed program
Creating a windowed program
 
Fundamentals of windows.64
Fundamentals of windows.64Fundamentals of windows.64
Fundamentals of windows.64
 
Creating a frame within an applet
Creating a frame within an appletCreating a frame within an applet
Creating a frame within an applet
 
Checkbox and checkbox group
Checkbox and checkbox groupCheckbox and checkbox group
Checkbox and checkbox group
 
Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttons
 
Working with color and font
Working with color and fontWorking with color and font
Working with color and font
 
Working with frames
Working with framesWorking with frames
Working with frames
 
‘ Final ‘ to avoid overriding
‘ Final ‘  to avoid overriding‘ Final ‘  to avoid overriding
‘ Final ‘ to avoid overriding
 
Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menus
 
This pointer .17
This pointer .17This pointer .17
This pointer .17
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
Command line arguments.21
Command line arguments.21Command line arguments.21
Command line arguments.21
 
Event handling63
Event handling63Event handling63
Event handling63
 

Similar to Basics of inheritance .22

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
 
Java Encapsulation and Inheritance
Java Encapsulation and Inheritance Java Encapsulation and Inheritance
Java Encapsulation and Inheritance AathikaJava
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxVikash Dúbēy
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptxrayanbabur
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritancemcollison
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfssusere9cd04
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritancekinan keshkeh
 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)It Academy
 
OO relationships between classes
OO relationships between classesOO relationships between classes
OO relationships between classesSujit Kumar
 

Similar to Basics of inheritance .22 (20)

Inheritance
Inheritance Inheritance
Inheritance
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
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
 
Java(inheritance)
Java(inheritance)Java(inheritance)
Java(inheritance)
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Java Encapsulation and Inheritance
Java Encapsulation and Inheritance Java Encapsulation and Inheritance
Java Encapsulation and Inheritance
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdf
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance
 
29c
29c29c
29c
 
29csharp
29csharp29csharp
29csharp
 
Unit 4
Unit 4Unit 4
Unit 4
 
INHERITANCES.pptx
INHERITANCES.pptxINHERITANCES.pptx
INHERITANCES.pptx
 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
 
OO relationships between classes
OO relationships between classesOO relationships between classes
OO relationships between classes
 
Inheritance
InheritanceInheritance
Inheritance
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Basics of inheritance .22

  • 1. Basics of Inheritance http://improvejava.blogspot.in/ 1
  • 2. Objectives On completion of this period, you would be able to learn • Inheritance • Inheritance basics • Visibility modifiers http://improvejava.blogspot.in/
  • 3. Recap In the previous lesson, you have learnt • Command-line arguments http://improvejava.blogspot.in/
  • 4. Inheritance • Inheritance is process by which one class gets the properties from an existing class • Object-oriented programming allows classes to inherit • Commonly used state and behavior from other classes • Inheritance is one of the principles of object oriented programming • Benefit of inheritance is the software reuse http://improvejava.blogspot.in/
  • 5. Inheritance Basics • Inheritance allows a software developer to derive a new class from an existing one • The existing class is called the parent class or superclass • The derived class is called the child class or subclass • Creates an is-a relationship The subclass is a more specific version of the original http://improvejava.blogspot.in/ 5
  • 6. Inheritance Basics contd.. • In Java a class that is inheriting is called a super class • A class that is inherited is called sub class • Sub class is a specialized version of super class • Sub class inherits all instance variables and methods defined by super class and can have its own elements • In Java one direct superclass, and each superclass has the potential for an unlimited number of subclasses http://improvejava.blogspot.in/ 6
  • 7. Inheritance Basics contd.. Book • In Fig. 22.1 Book is the existing class • Dictionary and Novel are Dictionary Novel sub classes of Book • Mystery and Romance are sub classes of Novel Mystery Romance Fig 22.1 Book inheritance http://improvejava.blogspot.in/ 7
  • 8. Discussion • What are the super and sub classes in Fig. 22.2 • Super class Bicycle • Sub classes MountBike RoadBike TandemBike Fig. 22.2 Bicycle example http://improvejava.blogspot.in/
  • 9. Discussion contd.. Object Class A Class D Class E Class B Class C Fig. 22.3 Another example • What are the super and sub classes in Fig. 22.3 • Super classes Object, Calls A, Class D • Sub classes Class A, Class B, Calss C, Class C, Class D http://improvejava.blogspot.in/
  • 10. Syntax • To inherit a class from another class use keyword “extends” Syntax class subclass-name extends superclass-name { ( body of the class ) } If a class B is inherited from class A class B extends A // A inherits B // Super class –A, Subclass- B http://improvejava.blogspot.in/
  • 11. Syntax contd... • Consider the above Bicycle example • Using the above syntax the inheritance can be shown as below class MountainBike extends Bicycle { // new fields and methods defining a mountain bike would go here } http://improvejava.blogspot.in/
  • 12. Example class A { int i,j; void showij() { System.out.println(“ i and j :” + I + “ “ + j); } } http://improvejava.blogspot.in/
  • 13. Example contd.. class B extends A {// creating subclass B from super class A int k; void showk() { System.out.println(“ k:” +k); } void sum() { System.out.println(“ i + j + k:” ( i+j+k)); } } http://improvejava.blogspot.in/
  • 14. Example contd.. class SimpleInheritance { public static void main( String args[]) { A superOb = new A(); B subOb = new B(); superOb.i = 10;// super class may be used by itself superOb.j = 20; System.out.println( “ Contents of superOb:”); superOb.showij(); System.out.println(); http://improvejava.blogspot.in/
  • 15. Example contd.. subOb.i = 7; // sub class has access to all public members subOb.j = 8; // of its super class subOj.k = 9; System.out.println(“ Contents of subOj:” ); subOb.showij(); subOj.showk(); System.out.println(); System.out.println( “ sum of i,j and k in subOb:”); subOb.sum(); } } http://improvejava.blogspot.in/
  • 16. Output Contents of superOb : i and j : 10 20 Contents of subOb : i and j : 7 8 k:9 sum of I,j and k in subOb : i + j + k: 24 http://improvejava.blogspot.in/
  • 17. Some Inheritance Details • An instance of a child class does not rely on an instance of a parent class • Hence we could create a Dictionary object without having to create a Book object first • Inheritance is a one-way street • The Book class cannot use variables or methods declared explicitly in the Dictionary class http://improvejava.blogspot.in/ 17
  • 18. Summary • In this class, we have discussed • Inheritance • Inheritance principle • Inheritance syntax • Super class • Sub class • Public, private and protected modifiers http://improvejava.blogspot.in/
  • 19. Quiz 1. A class which is inherited from other class is called a) Super class b) Sub class c) Anonymous class d) Object http://improvejava.blogspot.in/
  • 20. Quiz contd... 2. A class which used to inherits a class is called a) Sub class b) Super class c) Anonymous class d) object http://improvejava.blogspot.in/
  • 21. Quiz contd... 3. What is the keyword used to inherit a class a) create b) new c) this d) extends http://improvejava.blogspot.in/
  • 22. Quiz contd... 4.Public variables volatile the principle of a) Polymorphism b) Encapsulation c) Inheritance d) Overloading 22
  • 23. Quiz Contd.. 5. Variables and methods declared with following are inherited a) private b) public c) protected d) abstract http://improvejava.blogspot.in/
  • 24. Frequently Asked Questions • What is inheritance ? • Mention the key word to inherit a class from others • What is super class ? • What is sub class ? • Write about public modifier • Write about private modifier • What protected modifier • Write a program in Java using modifiers • Write a program in Java for inheriting a class from others http://improvejava.blogspot.in/