SlideShare a Scribd company logo
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

PPTX
B.sc CSIT 2nd semester C++ Unit5
PPT
New operator and methods.15
PPT
Displaying information within a window.68
PPT
Static.18
PPT
Lists and scrollbars
PPT
Static blocks, final variables .19
PPT
Multi level hierarchy
PPT
Super keyword.23
B.sc CSIT 2nd semester C++ Unit5
New operator and methods.15
Displaying information within a window.68
Static.18
Lists and scrollbars
Static blocks, final variables .19
Multi level hierarchy
Super keyword.23

Viewers also liked (14)

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

Similar to Basics of inheritance .22 (20)

DOCX
Sdtl assignment 03
PPTX
Inheritance
PDF
PDF
Chapter 04 inheritance
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
PPTX
Inheritance in java
PPTX
object oriented programming unit two ppt
PPT
Java inheritance
PPTX
Inheritance
PDF
javainheritance
PPTX
Java Inheritance - sub class constructors - Method overriding
PPT
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
PPT
Unit 3 Java
PPTX
java part 1 computer science.pptx
PPT
Inheritance & Polymorphism - 1
PPT
Inheritance and its necessity in java.ppt
PDF
Java programming -Object-Oriented Thinking- Inheritance
PPT
Ap Power Point Chpt7
PPTX
Inheritance
Sdtl assignment 03
Inheritance
Chapter 04 inheritance
OOPS_Unit2.inheritance and interface objected oriented programming
Inheritance in java
object oriented programming unit two ppt
Java inheritance
Inheritance
javainheritance
Java Inheritance - sub class constructors - Method overriding
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
Unit 3 Java
java part 1 computer science.pptx
Inheritance & Polymorphism - 1
Inheritance and its necessity in java.ppt
Java programming -Object-Oriented Thinking- Inheritance
Ap Power Point Chpt7
Inheritance
Ad

More from myrajendra (20)

PPT
Fundamentals
PPT
Data type
PPTX
Hibernate example1
PPTX
Jdbc workflow
PPTX
2 jdbc drivers
PPTX
3 jdbc api
PPTX
4 jdbc step1
PPTX
Dao example
PPTX
Sessionex1
PPTX
Internal
PPTX
3. elements
PPTX
2. attributes
PPTX
1 introduction to html
PPTX
Headings
PPTX
Forms
PPT
PPTX
Views
PPTX
Views
PPTX
Views
PPT
Starting jdbc
Fundamentals
Data type
Hibernate example1
Jdbc workflow
2 jdbc drivers
3 jdbc api
4 jdbc step1
Dao example
Sessionex1
Internal
3. elements
2. attributes
1 introduction to html
Headings
Forms
Views
Views
Views
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/