15. Inheritance
Thursday, September 10, 2009
In this post we are going to learn how inheritance promotes software reusability, the notations of superclass and subclass, using extends to create a class that inherits from another class and to use, protected modifier.
Inheritance enables a class to, Inherit data members and methods from another class and also reuse the functionalities and capabilities of the existing class by extending a new class from the existing class and adding new features to it.
Some of the key points which are associated with inheritance are given below.
1. The class that inherits the data members and methods from another class is known as the subclass.
2. The class from which the subclass inherits is known as the superclass.
3. The superclass is also referred to as the base class, and the subclass is referred to as the derived class.
4. We can create additional data members and methods to add more features in a subclass.
5. A superclass can also be a subclass of another class.
The direct superclass of a subclass specified by the keyword extends in the firstline of a class declaration is the superclass from which the subclass inherits.
An indirect superclass of a subclass is two or more levels up the class hierarchy from the subclass.
Implementing Different Types of Inheritance
Single level inheritance:
Derives a subclass from a single superclass. For example, subclasses B and C inherit the properties of a single superclass, A. The following figure shows the structure of single level inheritance.
The following syntax shows how to implement single level inheritance:
In the preceding syntax, the extends keyword is used to derive a subclass from a superclass.
Multilevel inheritance
Inherits the properties of another subclass. For example, Class A is a superclass for the Class B; and Class B is a superclass for the subclass, Class C. You can include any number of levels in multilevel inheritance. The following figure shows the structure of multilevel inheritance:
The following syntax shows how to implement multilevel inheritance
In the preceding syntax, class A is the superclass and class C is the subclass. The class B acts as a subclass for the class A and superclass for the class C.
Note: Java does not support multiple inheritance.
Have a look at the fallowing example to know more about Single Level Inheritance
Example
If we compile this program we can get the fallowing output
Books Information
Book Author: Steve
Book Title: Handbook
Book Price: 50
Number of pages: 350
Book Stock: 13
Software Books Information
Software Name: Windows
Software Version: Mary Peterson
Hardware Books Information
Hardware Title: Printers
Publisher Name: Tom Wilkins
Example for Multilevel Inheritance
If we compile the above program we can get the fallowing result.
Books Information
Book Price: 45
Number of pages: 450
Software Books Information
Software Name: Borland C++
Software Version: 5.0
C++ Books Information
Author Name: Lee Mitchell
Book Title: Programming using C++
Ok folks, that’s all for today, in the next post we are going to discuss more about inheritance and Overriding concepts, so keep an eye on the next post.
Inheritance enables a class to, Inherit data members and methods from another class and also reuse the functionalities and capabilities of the existing class by extending a new class from the existing class and adding new features to it.
Some of the key points which are associated with inheritance are given below.
1. The class that inherits the data members and methods from another class is known as the subclass.
2. The class from which the subclass inherits is known as the superclass.
3. The superclass is also referred to as the base class, and the subclass is referred to as the derived class.
4. We can create additional data members and methods to add more features in a subclass.
5. A superclass can also be a subclass of another class.
The direct superclass of a subclass specified by the keyword extends in the firstline of a class declaration is the superclass from which the subclass inherits.
An indirect superclass of a subclass is two or more levels up the class hierarchy from the subclass.
Implementing Different Types of Inheritance
Single level inheritance:
Derives a subclass from a single superclass. For example, subclasses B and C inherit the properties of a single superclass, A. The following figure shows the structure of single level inheritance.
The following syntax shows how to implement single level inheritance:
class A { } class B extends A { } class C extends A { }
In the preceding syntax, the extends keyword is used to derive a subclass from a superclass.
Multilevel inheritance
Inherits the properties of another subclass. For example, Class A is a superclass for the Class B; and Class B is a superclass for the subclass, Class C. You can include any number of levels in multilevel inheritance. The following figure shows the structure of multilevel inheritance:
The following syntax shows how to implement multilevel inheritance
class A { } class B extends A { } class C extends B { }
In the preceding syntax, class A is the superclass and class C is the subclass. The class B acts as a subclass for the class A and superclass for the class C.
Note: Java does not support multiple inheritance.
Have a look at the fallowing example to know more about Single Level Inheritance
Example
// Super class Book.java /** * * @author SUDARSAN */ class Book { //Declaring the data members. String author= "Steve"; String title = "Handbook"; int price = 50; int pages = 350; int stock = 13; //Defining the methods. public void show() { System.out.println(" "); System.out.println("\t Books Information"); System.out.println("\t Book Author: " + author); System.out.println("\t Book Title: " + title); System.out.println("\t Book Price: " + price); System.out.println("\t Number of pages: " + pages); System.out.println("\t Book Stock: " + stock); System.out.println(" "); } } // SubClass HardwareBook.java /** * * @author SUDARSAN */ class HardwareBook extends Book{ //Declaring the data members. String hardwareTitle = "Printers"; String publisher = "Tom Wilkins"; //Defining the method, showData() public void showData() { System.out.println(" "); System.out.println("\t Hardware Books Information"); System.out.println("\t Hardware Title: " + hardwareTitle); System.out.println("\t Publisher Name: " + publisher); System.out.println(" "); } } // SubClass SoftwareBook.java /** * * @author SUDARSAN */ class SoftwareBook extends Book{ //Declaring the data members. String softwareName = "Windows"; String softwareVersion = "Mary Peterson"; //Definition of method public void showDetails() { //Calling the method of Book class. show(); System.out.println("\t Software Books Information"); System.out.println("\t Software Name: " + softwareName); System.out.println("\t Software Version: " + softwareVersion); System.out.println(" "); } } //Main Class /** * * @author SUDARSAN */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here HardwareBook h = new HardwareBook(); SoftwareBook s = new SoftwareBook(); s.showDetails(); h.showData(); } }
If we compile this program we can get the fallowing output
Books Information
Book Author: Steve
Book Title: Handbook
Book Price: 50
Number of pages: 350
Book Stock: 13
Software Books Information
Software Name: Windows
Software Version: Mary Peterson
Hardware Books Information
Hardware Title: Printers
Publisher Name: Tom Wilkins
Example for Multilevel Inheritance
// SuperClass Book.java /** * * @author SUDARSAN */ class Book { //Declaring the data members. int price; int pages; //Defining the get() method. public void get(int mprice, int mpages) { price = mprice; pages = mpages; } public void show() { System.out.println(" "); System.out.println("\t Books Information"); System.out.println("\t Book Price: " + price); System.out.println("\t Number of pages: " + pages); System.out.println(" "); } } // SubClass SoftwareBook.java /** * * @author SUDARSAN */ class SoftwareBook extends Book{ //Declaring the data members. String softwareName; String softwareVersion; //Defining the methods. public void getDetails(String msoftwareName, String msoftwareVersion) { softwareName = msoftwareName; softwareVersion = msoftwareVersion; } public void showDetails() { System.out.println(" "); System.out.println("\t Software Books Information"); System.out.println("\t Software Name: " + softwareName); System.out.println("\t Software Version: " + softwareVersion); System.out.println(" "); } } // SubClass Cplus.java /** * * @author SUDARSAN */ class Cplus extends SoftwareBook{ //Declaring the data members. String author; String title; //Defining the methods. public void getData(String mauthor, String mtitle) { author = mauthor; title = mtitle; } public void showData() { show(); showDetails(); System.out.println(" "); System.out.println("\t C++ Books Information"); System.out.println("\t Author Name: " + author); System.out.println("\t Book Title: " + title); System.out.println(" "); } } // Main Class /** * * @author SUDARSAN */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Cplus c = new Cplus(); c.get(45, 450); // Calling the get() method of the Book class. // Calling the getDetails() method of the Software class. c.getDetails("Borland C++", "5.0"); // Calling the getData() method of the Cplus class. c.getData("Lee Mitchell", "Programming using C++"); c.showData(); } }
If we compile the above program we can get the fallowing result.
Books Information
Book Price: 45
Number of pages: 450
Software Books Information
Software Name: Borland C++
Software Version: 5.0
C++ Books Information
Author Name: Lee Mitchell
Book Title: Programming using C++
Ok folks, that’s all for today, in the next post we are going to discuss more about inheritance and Overriding concepts, so keep an eye on the next post.
Labels:
Inheritance