Computer Science
Java Inheritance
Java Inheritance is a mechanism in which one class acquires the properties and behaviors of another class. It allows the creation of a new class that is a modified version of an existing class. Inheritance promotes code reusability and helps in creating a hierarchical structure of classes.
Written by Perlego with AI-assistance
Related key terms
1 of 5
12 Key excerpts on "Java Inheritance"
- eBook - ePub
- Jeanne Boyarsky, Scott Selikoff(Authors)
- 2014(Publication Date)
- Sybex(Publisher)
Chapter 5 Class DesignOCA exam objectives covered in this chapter:
-
Working with Inheritance
- Describe inheritance and its benefits
- Develop code that demonstrates the use of polymorphism; including overriding and object type versus reference type
- Determine when casting is necessary
- Use super and this to access objects and constructors
- Use abstract classes and interfaces
In Chapter 1, “Java Building Blocks,” we introduced the basic definition for a class in Java. In Chapter 4, “Methods and Encapsulation,” we delved into constructors, methods, and modifiers, and showed how you can use them to build more structured classes. In this chapter, we'll take things one step further and show how class structure is one of the most powerful features in the Java language.At its core, proper Java class design is about code reusability, increased functionality, and standardization. For example, by creating a new class that extends an existing class, you may gain access to a slew of inherited primitives, objects, and methods. Alternatively, by designing a standard interface for your application, you ensure that any class that implements the interface has certain required methods defined. Finally, by creating abstract class definitions, you're defining a platform that other developers can extend and build on top of.Introducing Class Inheritance
When creating a new class in Java, you can define the class to inherit from an existing class.Inheritanceis the process by which the new child subclass automatically includes any public or protected primitives, objects, or methods defined in the parent class.For illustrative purposes, we refer to any class that inherits from another class as achild class, or a descendent of that class. Alternatively, we refer to the class that the child inherits from as theparent class - eBook - PDF
- Scott Selikoff, Jeanne Boyarsky(Authors)
- 2022(Publication Date)
- Sybex(Publisher)
Understanding Inheritance When creating a new class in Java, you can define the class as inheriting from an existing class. Inheritance is the process by which a subclass automatically includes certain members of the class, including primitives, objects, or methods, defined in the parent class. For illustrative purposes, we refer to any class that inherits from another class as a sub-class or child class , as it is considered a descendant of that class. Alternatively, we refer to the class that the child inherits from as the superclass or parent class , as it is considered an ancestor of the class. When working with other types, like interfaces, we tend to use the gen-eral terms subtype and supertype . You see this more in the next chapter. Declaring a Subclass Let’s begin with the declaration of a class and its subclass. Figure 6.1 shows an example of a superclass, Mammal , and subclass Rhinoceros . Understanding Inheritance 277 We indicate a class is a subclass by declaring it with the extends keyword. We don’t need to declare anything in the superclass other than making sure it is not marked final . More on that shortly. One key aspect of inheritance is that it is transitive. Given three classes [X, Y, Z], if X extends Y, and Y extends Z, then X is considered a subclass or descendant of Z. Likewise, Z is a superclass or ancestor of X. We sometimes use the term direct subclass or descendant to indicate the class directly extends the parent class. For example, X is a direct descendant only of class Y, not Z. In the last chapter, you learned that there are four access levels: public , protected , package, and private . When one class inherits from a parent class, all public and protected members are automatically available as part of the child class. If the two classes are in the same package, then package members are available to the child class. - eBook - PDF
- Joyce Farrell(Author)
- 2018(Publication Date)
- Cengage Learning EMEA(Publisher)
Every individual plant and pet has slightly different characteristics, but within a species, you can count on many consistent inherited attributes and behaviors. Similarly, the classes you create in object-oriented programming languages can inherit data and methods from existing classes. In Java and all object-oriented languages, inheritance is a mechanism that enables one class to acquire all the behaviors and attributes of another class, meaning that you can create a new class simply by indicating the ways in which it differs from a class that already has been developed and tested. When you create a class by making it inherit from another class, the new class automatically contains the data fields and methods of the original class. Diagramming Inheritance Using the UML Beginning with the first chapter of this book, you have been creating classes and instantiating them. Programmers and analysts sometimes use a graphical language to describe classes and object-oriented processes; this Unified Modeling Language (UML) consists of many types of diagrams, some of which can help illustrate inheritance. For example, consider the simple Employee class shown in Figure 10-1. The class contains two data fields, id and salary, and four methods: a get and set method for each field. Figure 10-2 shows a UML class diagram for the Employee class. A class diagram is a visual tool that provides you with an overview of a class. It consists of a rectangle divided into three sections—the top section contains the name of the class, the middle section contains the names and data types of the attributes, and the bottom section contains the methods. Only the method return type, name, and arguments are provided in the diagram—the instructions that make up the method body are omitted. Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. - eBook - PDF
- Harold Cabrera(Author)
- 2002(Publication Date)
- Syngress(Publisher)
Inheritance is an important part of both the C# and Java object models. In both Java and C# all classes are derived from a common base class. Java classes all subclass java.lang.Object , and C# classes all subclass Object . In object-oriented programming, inheritance is a strong and fundamental rela-tionship. For example, I have a son who plays in a high school marching band. In the marching band, he fulfills many roles but the role that is strong and funda-mental is that he is my son.The fact that he is a trumpet player, a section leader, or a sophomore is secondary. In an object-oriented system I would model the father-son relationship using inheritance. Later in this chapter we will learn how to model the weaker relationships. From the point of view of the band director the strong relationship is membership.The choice of fundamental relationship is not always obvious—it often depends upon your point of view.When you write C# code it will be your job to identify fundamental relationships and differen-tiate between these and weaker relationships. www.syngress.com www.syngress.com In either C# or Java you will often create a hierarchy of related classes. For example, a payroll system might have an Employee class. All companies have employees, so the Employee class is a reasonable abstraction. However, is this abstraction enough? Most payroll systems have more than just employees—they have many different kinds of employees.A payroll system might have salaried employees, commissioned employees and hourly employees. Some companies might even have truck drivers, lobbyists, and who knows what other kinds of employees. N OTE The Unified Modeling Language (UML) is a standard language for repre-senting object-oriented code, applications, or designs. Before UML there were several competing notational systems, but today almost all devel-opers use UML. This chapter will use UML class diagrams to show the relationships between classes. - eBook - PDF
- Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser(Authors)
- 2014(Publication Date)
- Wiley(Publisher)
For each pattern, be it for algorithm engineering or software engineering, we explain its general use and we illustrate it with at least one concrete example. 64 Chapter 2. Object-Oriented Design 2.2 Inheritance A natural way to organize various structural components of a software package is in a hierarchical fashion, with similar abstract definitions grouped together in a level-by-level manner that goes from specific to more general as one traverses up the hierarchy. An example of such a hierarchy is shown in Figure 2.3. Using mathematical notations, the set of houses is a subset of the set of buildings, but a superset of the set of ranches. The correspondence between levels is often referred to as an “is a” relationship, as a house is a building, and a ranch is a house. Building Low-rise Apartment High-rise Apartment Two-story House Ranch Skyscraper Commercial Building House Apartment Figure 2.3: An example of an “is a” hierarchy involving architectural buildings. A hierarchical design is useful in software development, as common function- ality can be grouped at the most general level, thereby promoting reuse of code, while differentiated behaviors can be viewed as extensions of the general case. In object-oriented programming, the mechanism for a modular and hierarchical orga- nization is a technique known as inheritance. This allows a new class to be defined based upon an existing class as the starting point. In object-oriented terminology, the existing class is typically described as the base class, parent class, or super- class, while the newly defined class is known as the subclass or child class. We say that the subclass extends the superclass. When inheritance is used, the subclass automatically inherits, as its starting point, all methods from the superclass (other than constructors). The subclass can differentiate itself from its superclass in two ways. It may augment the superclass by adding new fields and new methods. - eBook - PDF
C++ Programming
Program Design Including Data Structures
- D. Malik(Author)
- 2017(Publication Date)
- Cengage Learning EMEA(Publisher)
In C 11 , the mechanism that allows us to accomplish this task is the principle of inheritance. Inheritance is an “is-a” relationship; for instance, “every employee is a person.” Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Inheritance | 745 1 1 Inheritance lets us create new classes from existing classes. The new classes that we create from the existing classes are called the derived classes ; the existing classes are called the base classes . The derived classes inherit the properties of the base classes. So rather than create completely new classes from scratch, we can take advantage of inheritance and reduce software development complexity. Each derived class, in turn, can become a base class for a future derived class. Inheri-tance can be either single inheritance or multiple inheritance. In single inheritance , the derived class is derived from a single base class; in multiple inheritance , the derived class is derived from more than one base class. This chapter concentrates on single inheritance. FIGURE 11-1 Inheritance hierarchy circle rectangle square shape Inheritance can be viewed as a treelike, or hierarchical, structure wherein a base class is shown with its derived classes. Consider the tree diagram shown in Figure 11-1. In this diagram, shape is the base class. The class es circle and rectangle are derived from shape , and the class square is derived from rectangle . Every circle and every rectangle is a shape . Every square is a rectangle . The general syntax of a derived class is: in which memberAccessSpecifier is public , protected , or private . When no memberAccessSpecifier is specified, it is assumed to be a private inheritance. (We will discuss protected inheritance later in this chapter.) class className: memberAccessSpecifier baseClassName { member list }; Copyright 2018 Cengage Learning. - eBook - PDF
- Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser(Authors)
- 2014(Publication Date)
- Wiley(Publisher)
For each pattern, be it for algorithm engineering or software engineering, we explain its general use and we illustrate it with at least one concrete example. 58 Chapter 2. Object-Oriented Design 2.2 Inheritance A natural way to organize various structural components of a software package is in a hierarchical fashion, with similar abstract definitions grouped together in a level-by-level manner that goes from specific to more general as one traverses up the hierarchy. An example of such a hierarchy is shown in Figure 2.3. Using mathematical notations, the set of houses is a subset of the set of buildings, but a superset of the set of ranches. The correspondence between levels is often referred to as an “is a” relationship, as a house is a building, and a ranch is a house. Figure 2.3: An example of an “is a” hierarchy involving architectural buildings. A hierarchical design is useful in software development, as common function- ality can be grouped at the most general level, thereby promoting reuse of code, while differentiated behaviors can be viewed as extensions of the general case. In object-oriented programming, the mechanism for a modular and hierarchical orga- nization is a technique known as inheritance. This allows a new class to be defined based upon an existing class as the starting point. In object-oriented terminology, the existing class is typically described as the base class, parent class, or super- class, while the newly defined class is known as the subclass or child class. We say that the subclass extends the superclass. When inheritance is used, the subclass automatically inherits, as its starting point, all methods from the superclass (other than constructors). The subclass can differentiate itself from its superclass in two ways. It may augment the superclass by adding new fields and new methods. It may also specialize existing behaviors by providing a new implementation that overrides an existing method. - eBook - PDF
Java Concepts
Late Objects
- Cay S. Horstmann(Author)
- 2016(Publication Date)
- Wiley(Publisher)
438 Objects from related classes usually share common behavior. For example, shovels, rakes, and clippers all perform gardening tasks. In this chapter, you will learn how the notion of inheritance expresses the relationship between specialized and general classes. By using inheritance, you will be able to share code between classes and provide services that can be used by multiple classes. 9.1 Inheritance Hierarchies In object-oriented design, inheritance is a relationship between a more general class (called the superclass) and a more specialized class (called the subclass). The subclass inherits data and behavior from the superclass. For example, consider the relation- ships between different kinds of vehicles depicted in Figure 1. Every car is a vehicle. Cars share the common traits of all vehicles, such as the abil- ity to transport people from one place to another. We say that the class Car inherits from the class Vehicle. In this relationship, the Vehicle class is the superclass and the Car class is the subclass. In Figure 2, the superclass and subclass are joined with an arrow that points to the superclass. Suppose we have an algorithm that manipulates a Vehicle object. Because a car is a special kind of vehicle, we can use a Car object in such an algorithm, and it will work correctly. The substitution principle states that you can always use a subclass object when a superclass object is expected. For example, consider a method that takes an argument of type Vehicle: void processVehicle(Vehicle v) A subclass inherits data and behavior from a superclass. You can always use a subclass object in place of a superclass object. Figure 1 An Inheritance Hierarchy of Vehicle Classes Vehicle Motorcycle Car Truck Sedan SUV © Lisa Thornberg/iStockphoto. - 438 Objects from related classes usually share common behavior. For example, shovels, rakes, and clippers all perform gardening tasks. In this chapter, you will learn how the notion of inheritance expresses the relationship between specialized and general classes. By using inheritance, you will be able to share code between classes and provide services that can be used by multiple classes. 9.1 Inheritance Hierarchies In object-oriented design, inheritance is a relationship between a more general class (called the superclass ) and a more specialized class (called the subclass ). The subclass inherits data and behavior from the superclass. For example, consider the relation-ships between different kinds of vehicles depicted in Figure 1. Every car is a vehicle. Cars share the common traits of all vehicles, such as the abil-ity to transport people from one place to another. We say that the class Car inherits from the class Vehicle . In this relationship, the Vehicle class is the superclass and the Car class is the subclass. In Figure 2, the superclass and subclass are joined with an arrow that points to the superclass. Suppose we have an algorithm that manipulates a Vehicle object. Because a car is a special kind of vehicle, we can use a Car object in such an algorithm, and it will work correctly. The substitution principle states that you can always use a subclass object when a superclass object is expected. For example, consider a method that takes an argument of type Vehicle : void processVehicle(Vehicle v) A subclass inherits data and behavior from a superclass. You can always use a subclass object in place of a superclass object. Figure 1 An Inheritance Hierarchy of Vehicle Classes Vehicle Motorcycle Car Truck Sedan SUV © Lisa Thornberg/iStockphoto.
- eBook - PDF
Microsoft® Visual C# 2015
An Introduction to Object-Oriented Programming
- Joyce Farrell, , , (Authors)
- 2015(Publication Date)
- Cengage Learning EMEA(Publisher)
Every plant and pet has slightly different characteristics, but within a species, you can count on many consistent inherited attributes and behaviors. In other words, you can reuse the knowledge you gain about general categories and apply it to more specific categories. Similarly, the classes you create in object-oriented programming languages can inherit data and methods from existing classes. When you create a class by making it inherit from another class, you are provided with data fields and methods automatically; you can reuse fields and methods that are already written and tested. You already know how to create classes and how to instantiate objects from those classes. For example, consider the Employee class in Figure 10-1. The class contains two data fields, idNum and salary , as well as properties that contain accessors for each field and a method that creates an Employee greeting. Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 425 Understanding Inheritance class Employee { private int idNum; private double salary; public int IdNum { get { return idNum; } set { idNum = value ; } } public double Salary { get { return salary; } set { salary = value ; } } public string GetGreeting() { string greeting = Hello. - No longer available |Learn more
- (Author)
- 2014(Publication Date)
- Learning Press(Publisher)
Subclasses usually consist of several kinds of modifications (customizations) to their respective superclasses: addition of new instance variables, addition of new methods and overriding of existing methods to support the new instance variables. Conceptually, a superclass should be considered as a common part of its subclasses. This factoring of commonality is one mechanism for providing reuse. Thus, extending a superclass by modifying the existing class is also likely to narrow its applicability in various situations. In object-oriented design, careful balance between applicability and functionality of superclasses should be considered. Subclassing is different from subtyping in that subtyping deals with common behaviour whereas subclassing is concerned with common structure. Some programming languages (for example C++) allow multiple inheritance - they allow a child class to have more than one parent class. This technique has been criticized by some for its unnecessary complexity and being difficult to implement efficiently, though some projects have certainly benefited from its use. Java, for example has no multiple ________________________ WORLD TECHNOLOGIES ________________________ inheritance, as its designers felt that it would add unnecessary complexity. Java instead allows inheriting from multiple pure abstract classes (called interfaces in Java). Sub- and superclasses are considered to exist within a hierarchy defined by the inheritance relationship. If multiple inheritance is allowed, this hierarchy is a directed acyclic graph (or DAG for short), otherwise it is a tree. The hierarchy has classes as nodes and inheritance relationships as links. The levels of this hierarchy are called layers or levels of abstraction. Classes in the same level are more likely to be associated than classes in different levels. There are two slightly different points of view as to whether subclasses of the same class are required to be disjoint. - Joyce Farrell(Author)
- 2017(Publication Date)
- Cengage Learning EMEA(Publisher)
Every plant and pet has slightly different characteristics, but within a species, you can count on many consistent inherited attributes and behaviors. In other words, you can reuse the knowledge you gain about general categories and apply it to more specific categories. Similarly, the classes you create in object-oriented programming languages can inherit data and methods from existing classes. When you create a class by making it inherit from another class, you are provided with data fields and methods automatically; you can reuse fields and methods that are already written and tested. You already know how to create classes and how to instantiate objects from those classes. For example, consider the Employee class in Figure 10-1. The class contains a field for the employee’s salary as well as two properties, IdNum and Salary . The IdNum property is an auto-implemented property, but the Salary property is not auto-implemented—it is created manually to ensure that an Employee ’s salary cannot be less than a specified limit. The class also contains a method that creates an Employee greeting. Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 423 Understanding Inheritance class Employee { private double salary; public int IdNum { get ; set ;} public double Salary { get { return salary; } set { const double MIN = 15_000; if ( value < MIN) salary = MIN; else salary = value; } public string GetGreeting() { string greeting = Hello.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.











