Basics of Inheritance                        http://improvejava.blogspot.in/                                              ...
ObjectivesOn completion of this period, you would be able tolearn• Inheritance• Inheritance basics• Visibility modifiers  ...
RecapIn the previous lesson, you have learnt• Command-line arguments                  http://improvejava.blogspot.in/
Inheritance• Inheritance is process by which one class gets theproperties from an existing class• Object-oriented programm...
Inheritance Basics• Inheritance allows a software developer to  derive a new class from an existing one• The existing clas...
Inheritance Basics   contd..   • In Java a class that is inheriting is called a super class   • A class that is inherited ...
Inheritance Basics                       contd..                                                         Book• In Fig. 22....
Discussion• What are the super and sub classes in Fig. 22.2• Super class     Bicycle• Sub classes      MountBike      Road...
Discussion                           contd..                                                             Object           ...
Syntax• To inherit a class from another class use keyword                        “extends”Syntaxclass subclass-name extend...
Syntax contd...• Consider the above Bicycle example• Using the above syntax the inheritance can be shown as below class Mo...
Exampleclass A {    int i,j;    void showij() {           System.out.println(“ i and j :” + I + “ “ + j);    }}           ...
Example contd..class B extends A {// creating subclass B from super class A    int k;    void showk() {             System...
Example                  contd..class SimpleInheritance {    public static void main( String args[]) {           A superOb...
Example                          contd..        subOb.i = 7; // sub class has access to all public members        subOb.j ...
OutputContents of superOb :    i and j : 10 20Contents of subOb :     i and j : 7 8      k:9sum of I,j and k in subOb :   ...
Some Inheritance Details• An instance of a child class does not rely on an instance  of a parent class  • Hence we could c...
Summary• In this class, we have discussed   • Inheritance   • Inheritance principle   • Inheritance syntax   • Super class...
Quiz1. A class which is inherited from other class is calleda) Super classb) Sub classc) Anonymous classd) Object         ...
Quiz              contd...2. A class which used to inherits a class is calleda)    Sub classb)    Super classc)    Anonymo...
Quiz               contd...3. What is the keyword used to inherit a class   a) create   b) new   c) this   d) extends     ...
Quiz       contd...4.Public variables volatile the principle ofa) Polymorphismb) Encapsulationc) Inheritanced) Overloading...
Quiz Contd..5. Variables and methods declared with following   are inheriteda) privateb) publicc) protectedd) abstract    ...
Frequently Asked Questions• What is inheritance ?• Mention the key word to inherit a class from others• What is super clas...
Upcoming SlideShare
Loading in...5
×

Basics of inheritance .22

309

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
309
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Basics of inheritance .22

  1. 1. Basics of Inheritance http://improvejava.blogspot.in/ 1
  2. 2. ObjectivesOn completion of this period, you would be able tolearn• Inheritance• Inheritance basics• Visibility modifiers http://improvejava.blogspot.in/
  3. 3. RecapIn the previous lesson, you have learnt• Command-line arguments http://improvejava.blogspot.in/
  4. 4. Inheritance• Inheritance is process by which one class gets theproperties 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 orientedprogramming• Benefit of inheritance is the software reuse http://improvejava.blogspot.in/
  5. 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. 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 subclasseshttp://improvejava.blogspot.in/ 6
  7. 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. 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. 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. 10. Syntax• To inherit a class from another class use keyword “extends”Syntaxclass 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. 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. 12. Exampleclass A { int i,j; void showij() { System.out.println(“ i and j :” + I + “ “ + j); }} http://improvejava.blogspot.in/
  13. 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. 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. 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. 16. OutputContents of superOb : i and j : 10 20Contents of subOb : i and j : 7 8 k:9sum of I,j and k in subOb : i + j + k: 24 http://improvejava.blogspot.in/
  17. 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. 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. 19. Quiz1. A class which is inherited from other class is calleda) Super classb) Sub classc) Anonymous classd) Object http://improvejava.blogspot.in/
  20. 20. Quiz contd...2. A class which used to inherits a class is calleda) Sub classb) Super classc) Anonymous classd) object http://improvejava.blogspot.in/
  21. 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. 22. Quiz contd...4.Public variables volatile the principle ofa) Polymorphismb) Encapsulationc) Inheritanced) Overloading 22
  23. 23. Quiz Contd..5. Variables and methods declared with following are inheriteda) privateb) publicc) protectedd) abstract http://improvejava.blogspot.in/
  24. 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/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×