In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes. Show
Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class). Excepting Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. The Java Platform Class HierarchyThe All Classes in the Java Platform are Descendants of Object At the top of the hierarchy, An Example of InheritanceHere is the sample code for a possible implementation of a public class Bicycle { // the Bicycle class has three fields public int cadence; public int gear; public int speed; // the Bicycle class has one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } } A class declaration for a public class MountainBike extends Bicycle { // the MountainBike subclass adds one field public int seatHeight; // the MountainBike subclass has one constructor public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } // the MountainBike subclass adds one method public void setHeight(int newValue) { seatHeight = newValue; } }
What You Can Do in a SubclassA subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:
The following sections in this lesson will expand on these topics. Private Members in a SuperclassA subclass does not inherit the A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass. Casting ObjectsWe have seen that an object is of the data type of the class from which it was instantiated. For example, if we write public MountainBike myBike = new MountainBike(); then
The reverse is not necessarily true: a Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance and implementations. For example, if we write Object obj = new MountainBike(); then If, on the other hand, we write MountainBike myBike = obj; we would get a compile-time error because MountainBike myBike = (MountainBike)obj; This cast inserts a runtime check that Note: You can make a logical test as to the type of a particular object using the if (obj instanceof MountainBike) { MountainBike myBike = (MountainBike)obj; } Here the When class B is derived from A and class C is derived from the this kind of inheritance is known as?The inheritance in which a class can be derived from another derived class is known as Multilevel Inheritance. Suppose there are three classes A, B, and C. A is the base class that derives from class B. So, B is the derived class of A.
When a class inherits from a derived class it is known as?The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.
When a class is derived from many base classes it is called?Hierarchical inheritance. This is where one class serves as a superclass (base class) for more than one sub class. For example, a parent class, A, can have two subclasses B and C.
Which is the correct class definition for class C class C inherits from A & B classes Mcq?The correct answer is option 4. Concept: Inheritance means acquiring the properties of base class by the derived class. There are three accessing modes in inheritance: private, protected and public.
|