Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and behaviors from another class. It establishes a hierarchical relationship between classes, where one class, known as the derived class or subclass, inherits characteristics from another class called the base class or superclass.

 

The subclass inherits the attributes (data members) and methods (functions) of the superclass, and it can also add its own specific attributes and methods or override the inherited ones. This mechanism promotes code reuse, modularity, and extensibility in software development.

Inheritance is typically represented using an "is-a" relationship. For example, if we have a base class called Animal, we can create subclasses such as Dog, Cat, and Bird, where each subclass inherits common attributes and behaviors from the Animal class. The subclasses can then add their specific attributes and behaviors, such as bark() in Dog or fly() in Bird.

Key concepts and terms related to inheritance include:

  1. Base class/superclass: The class from which other classes inherit properties and behaviors. It defines a generalized set of attributes and methods.

  2. Derived class/subclass: The class that inherits properties and behaviors from the base class. It specializes the base class by adding or modifying attributes and methods.

  3. Inheritance hierarchy: The structure formed by multiple levels of inheritance, where a derived class can itself become a base class for another subclass. This hierarchy allows for the creation of more specialized classes.

  4. Single inheritance: A form of inheritance where a subclass inherits from only one base class.

  5. Multiple inheritance: A form of inheritance where a subclass can inherit from multiple base classes. This feature is not available in all programming languages.

  6. Polymorphism: The ability of objects of different classes, which are related through inheritance, to be treated as objects of the base class. Polymorphism allows for the use of common methods across different subclasses.

In summary, inheritance is a powerful concept in OOP that facilitates code reuse, promotes modularity, and enables the creation of class hierarchies with specialized behavior. It provides a way to model real-world relationships and organize code in a more logical and maintainable manner.

Demo

Publication (base class)

package InheritanceDemo; /** * @author Dr. Kevin Roark * A general Class for publications - our base class * */ public class Publication { //private Variables private String title; private String author; private int numberOfPages; /* Getters and Setters */ public String getTitle() { return title; } protected void setTitle(String title) { this.title = title; } protected String getAuthor() { return author; } protected void setAuthor(String author) { this.author = author; } public int getNumberOfPages() { return numberOfPages; } public void setNumberOfPages(int numberOfPages) { this.numberOfPages = numberOfPages; } /* Constructors */ protected Publication(String title, String author, int numberOfPages) { super(); this.title = title; this.author = author; this.numberOfPages = numberOfPages; } /* Class Methods */ protected String PrintInformation() { String myReturn = ""; myReturn += "Title: " + this.getTitle() + "\n"; myReturn += "Author: " + this.getAuthor() + "\n"; myReturn += "Number of Pages: " + this.getNumberOfPages() + "\n"; return myReturn; } } // end of Class

Inherited Classes

package InheritanceDemo; /** * @author Dr. Kevin Roark * A derived class from Publications * */ import java.time.LocalDate; public class Magazine extends Publication { //variables for Magazine private String subscriptionType; private LocalDate subscritionDate; //Getters and Setters /** * @return the subscriptionType */ public String getSubscriptionType() { return subscriptionType; } /** * @param subscriptionType the subscriptionType to set */ public void setSubscriptionType(String subscriptionType) { this.subscriptionType = subscriptionType; } /** * @return the subscritionDate */ public LocalDate getSubscritionDate() { return subscritionDate; } /** * @param subscritionDate the subscritionDate to set */ public void setSubscritionDate(LocalDate subscritionDate) { this.subscritionDate = subscritionDate; } //Constructor /** * @param title * @param author * @param numberOfPages * @param subscriptionType * @param subscritionDate */ public Magazine(String title, String author, int numberOfPages, String subscriptionType, LocalDate subscritionDate) { super(title, author, numberOfPages); this.subscriptionType = subscriptionType; this.subscritionDate = subscritionDate; } //Methods public String PrintInformation() { String myReturn = super.PrintInformation(); myReturn += "Subscription Type: " + getSubscriptionType() + "\n"; myReturn += "Publish Date: " + this.getSubscritionDate().toString() + "\n"; return myReturn; } }
package InheritanceDemo; /** * @author Dr. Kevin Roark * A Textbook class that inherits from Publications - our base class * */ public class TextBook extends Publication{ private String version; private String subjectDomain; //Getters and Setters /** * @return the version */ public String getVersion() { return version; } /** * @param version the version to set */ public void setVersion(String version) { this.version = version; } /** * @return the subjectDomain */ public String getSubjectDomain() { return subjectDomain; } /** * @param subjectDomain the subjectDomain to set */ public void setSubjectDomain(String subjectDomain) { this.subjectDomain = subjectDomain; } //Constructors /** * @param title * @param author * @param numberOfPages * @param version * @param subjectDomain */ public TextBook(String title, String author, int numberOfPages, String version, String subjectDomain) { super(title, author, numberOfPages); this.version = version; this.subjectDomain = subjectDomain; } //Functions / Methods public String PrintInformation() { String myReturn = super.PrintInformation(); myReturn += "Version: " + this.getVersion() + "\n"; myReturn += "Subject Domain: " + this.getSubjectDomain() + "\n"; return myReturn; } }

Driver