Category Archive for: Inheritance

Using final with inheritance

Using final with inheritance The keyword final has three Uses. First, it can be used to create the equivalent of a  .named constant. This use was described in the preceding chapter. The other two uses  of final apply to inheritance. Both are examined here Using final to Prevent Overriding While method overriding is one of most powerful features,…

Read More →

Applying Method Overriding

Applying Method Overriding Let’s look at a more practical example that uses method overriding. The following  program creates a super class called Figure that stores the dimensions of various two-dimensional objects. It also defines a method called area that computes the area of an object. The program derives two sub classes from Figure. The first is Rectangle and…

Read More →

When Constructors are Called

When Constructors are Called When a class hierarchy is created, in what order are the constructors for the classes that, make up the called ?For example, given a subclass called B and a super class called A, is. As constructor called before B’s, or vice The answer is .that in a class hierarchy, constructors are called in order…

Read More →

Creating a Muiti level Hierarchy

Creating a Mufti level Hierarchy Up to this point, we have been using simple class hierarchies that consist of only a super class and a subclass. However, you can build hierarchies that contain as many layers of inheritance as you like. As mentioned, it is perfectly acceptable to use a subclass as a super class of another. For example, given…

Read More →

A Super class Variable Can Reference a Subclass

A Super class Variable Can Reference a Subclass  A reference variable of a super class can be assigned a reference to any subclass derived from that super class. You will find this aspect of inheritance quite useful in a variety of situations. For example, consider the following Here, weight box is a Terence ‘to Box Weight objects, and…

Read More →

Member Access and Inheritance

Member Access and Inheritance Although a subclass includes all of the members of its super class, it cannot access those members of the super class that have been declared as private. For example, consider the following simple class hierarchy. This program will not compile because the reference to  method of B causes an access violation, Since j is…

Read More →

Inheritance

Inheritance Inheritances one of the cornerstones of object-oriented programming because it allow the creation of hierarchical classifications, Using inheritance, you can create a general class that defines traits common 10 a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it, In the terminology of a…

Read More →

Back to Top