Examples of creating class hierarchies using inheritance. Classes Student=>Aspirant, Book=>BookGenre=>BookGenrePubl
This topic discusses two examples of class development using inheritance. Each example contains comments explaining the features of the corresponding piece of code. When creating class hierarchies, polymorphism is not used.
The topic will be useful to novice programmers in learning the basics of programming classes that use inheritance.
Contents
- 1. An example of creating a class hierarchy. Classes Student, Aspirant
- 2. Implementation inheritance using the Book class as an example
- Related topics
Search other websites:
1. An example of creating a class hierarchy. Classes Student, Aspirant
The example demonstrates:
- use of the protected access modifier during inheritance;
- use of the base keyword in case of calling the base class constructor.
1.1. Task
Create a base class Student, which will contain information about the student (last name, course of study, number of the gradebook). Using the inheritance mechanism, implement the Aspirant class (a graduate student is a student who is preparing to defend a candidate dissertation). The Aspirant class is derived from the Student class.
In the Student and Aspirant classes, you must implement the following elements:
- class constructors with the appropriate number of parameters. In the Aspirant class, you must use the base keyword to access the methods of the Student class;
- get/set properties for accessing class fields.
In the Student and Aspirant classes, you must implement the following elements:
- class constructors with the corresponding number of parameters. In the Aspirant class, you must use the base keyword to access the methods of the Student class;
- get/set properties for accessing class fields;
- method Print(), which displays information about the contents of the fields of the class.
⇑
1.2. Solution. Program text
The problem is solved for an application such as Console Application. The following is the full text of the program.
using static System.Console; namespace ConsoleApp1 { // Base class Student, contains information about the student public class Student { // 1. Class fields - declared as protected - are visible in derived classes, // and are invisible from an instance of the class protected string name; // Student's name protected int course; // Course of Study protected string gradeBook; // Grade Book Number // 2. Class constructor with 3 parameters public Student(string Name, int course, string gradeBook) { this.Name = Name; this.course = course; this.gradeBook = gradeBook; } // 3. Properties to the class fields access public string Name { get { return name; } set { name = value; } } public int Course { get { return course; } set { course = value; } } public string GradeBook { get { return gradeBook; } set { gradeBook = value; } } // 4. Method Print() - display the fields on the screen public void Print() { WriteLine("The values of fields are:"); WriteLine($"Name = {name}"); WriteLine($"Course = {course}"); WriteLine($"GradeBook = {gradeBook}"); } } // Class Aspirant - inherits possibilities of Student class public class Aspirant : Student { // 1. Internal field protected string topic; // Theme of Ph.D. dissertation // 2. The constructor of Aspirant class - using the base keyword, // refers to the constructor of the base class Student. public Aspirant(string name, int course, string gradeBook, string topic) : base(name, course, gradeBook) { // You can modify the protected members of the base class base.name = name; // access to the field name of class Student using base this.course = course; // access to the Student.course field using 'this' this.gradeBook = gradeBook; this.topic = topic; // initialization of the internal field of the Aspirant class } // 3. Property to access the 'topic' field public string Topic { get { return topic; } set { topic = value; } } // 4. Print() method - print fields of the Aspirant class // The name of this method overrides the name of the Student.Print() method, // so new is preceded by the name of the method public new void Print() // new - overriding method of the base class { base.Print(); // invoke method Print() of the base class WriteLine($"Topic = {topic}"); } } class Program { static void Main(string[] args) { // Demonstration of working with Student and Aspirant classes // 1. Declare an instance of class Student Student st1 = new Student("Ivanov I.I.", 2, "0519"); // 2. Display fields of the Student class WriteLine("The instance of st1:"); st1.Print(); // 3. Declare the instance of Aspirant class // When declaring, the get property is used of the st1 instance Aspirant asp1 = new Aspirant(st1.Name, st1.Course, st1.GradeBook, "Hello world!"); // 4. Invoke the Print() method of instance asp1 WriteLine("---------------------"); WriteLine("The instance of asp1:"); asp1.Print(); } } }
⇑
1.3. The result of the program
The instance of st1: The values of fields are: Name = Ivanov I.I. Course = 2 GradeBook = 0519 --------------------- The instance of asp1: The values of fields are: Name = Ivanov I.I. Course = 2 GradeBook = 0519 Topic = Hello world!
⇑
2. Implementation inheritance using the Book class as an example
2.1. Task
The class Book is defined that describes the book. The class contains the following elements:
- title of book;
- surname and name of the author;
- the cost of the book.
The following methods must be implemented in the Book class:
- constructor with 3 parameters;
- get/set properties for accessing class fields;
- the Print() method, which displays information about the book.
Develop a BookGenre class that inherits the capabilities of the Book class and adds a genre field. In the BookGenre class, implement the following elements:
- constructor with 4 parameters – implements a call to the constructor of the base class;
- property get/set access to the internal field of the class;
- the Print() method, which calls the Print() method of the base class Book to display information about all the fields of the class.
Develop the BookGenrePubl class, which is inherited from the BookGenre class. This class adds a field that contains information about the publisher. In the BookGenrePubl class, implement the following elements:
- constructor with 5 parameters;
- get/set property to access the internal field of the class;
- the Print() method, which calls the base class method of the same name and displays information about the publisher.
Make the BookGenrePubl class such that it cannot be inherited.
⇑
2.2. Solution. The text of the program
using static System.Console; namespace ConsoleApp1 { // Class Book - base class class Book { // 1. Internal field of class string title; // the title of book string author; // the name of author double price; // book cost // 2. Constructor with 3 parameters public Book(string _title, string _author, double _price) { title = _title; author = _author; // correct the book cost if (price < 0) price = 0.0; else price = _price; } // 3. Properties of type get/set to access the book's fields public string Title { get { return title; } set { title = value; } } public string Author { get { return author; } set { author = value; } } public double Price { get { return price; } set { if (price < 0) price = 0.0; else price = value; } } // 4. Method Print() - display the class fields public void Print() { WriteLine("title = {0}, author = {1}, price = {2:f2}", title, author, price); } } // The class that inherits the Book class and adds the genre to the book. class BookGenre : Book { // 1. The internal field is the genre to which the book belongs string genre; // 2. Constructor with 4 parameters. // Invokes the constructor of the base class using base (...). public BookGenre(string _title, string _author, double _price, string _genre) : base(_title, _author, _price) { genre = _genre; } // 3. The property of access to the genre field public string Genre { get { return genre; } set { genre = value; } } // 4. Print() method - calls the base class method. // The method header uses the new keyword. This is the recommendation // of the compiler - to emphasize that this method hides the method // of the same name of the base class. public new void Print() { base.Print(); // call the Print () method of the base class Book WriteLine("genre = {0}", genre); } } // The class that inherits the BookGenre class, // adds a publisher field to the class hierarchy. // The sealed keyword is used before the class - this means // that this class cannot be inherited by other classes. sealed class BookGenrePubl : BookGenre { // 1. Internal field - information about publisher private string publisher; // 2. Constructor with 5 parameters - Calls the constructor // of the BookGenre() base class using the base keyword. public BookGenrePubl(string _title, string _author, double _price, string _genre, string _publisher) : base(_title, _author, _price, _genre) { publisher = _publisher; } // 3. Property to access the publisher field public string Publisher { get { return publisher; } set { publisher = value; } } // 4. Print() method - calls the base class method. // It is recommended to use the new keyword in the method declaration, // since this method "hides" the base class method. public new void Print() { base.Print(); WriteLine("publisher = {0}", publisher); } } class Program { static void Main(string[] args) { // 1. Declare the instance of class Book Book b1 = new Book("Title - 01", "Author - 01", 122.25); // 2. Display the values of fields of instance class b1 b1.Print(); // 3. Declare the instance of BookGenre class BookGenre bg1 = new BookGenre("Title - BookGenre", "Author - BookGenre", 200.33, "Story"); // 4. Print the values of bg1 instance WriteLine("-------------------"); bg1.Print(); // 5. Declare the instance of BookGenrePubl class BookGenrePubl bp1 = new BookGenrePubl("Title - BookGenrePubl", "Author - BookGenrePubl", 300.55, "Story", "Pupkin Inc."); // 6. Display the values of fields of bp1 instance WriteLine("-------------------"); bp1.Print(); } } }
⇑
2.3. The result of the program
title = Title - 01, author = Author - 01, price = 122.25 ------------------- title = Title - BookGenre, author = Author - BookGenre, price = 200.33 genre = Story ------------------- title = Title - BookGenrePubl, author = Aurhor - BookGenrePubl, price = 300.55 genre = Story publisher = Pupkin Inc.
⇑
Related topics
- Inheritance. Basic concepts. Advantages and disadvantages. General form. The simplest examples. Access modifier protected
- Using constructors in classes for inheritance. The base keyword. Examples
- Access to elements of a base class from an inherited class. Keywords base, new. Examples
⇑