Using the Keyword super
Accessing Superclass Members
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keywordsuper. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass:public class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
Subclass, that overrides printMethod():public class Subclass extends Superclass {
// overrides printMethod in Superclass
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}
}
Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod() inherited from Superclass, Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:Printed in Superclass. Printed in Subclass
Subclass Constructors
The following example illustrates how to use thesuper keyword to invoke a superclass's constructor. Recall from the Bicycle example that MountainBike is a subclass of Bicycle. Here is the MountainBike(subclass) constructor that calls the superclass constructor and then adds initialization code of its own:public MountainBike(int startHeight,
int startCadence,
int startSpeed,
int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
The syntax for calling a superclass constructor is
super();
super(parameter list);
super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.
Object does have such a constructor, so if Object is the only superclass, there is no problem.Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.Types of Inheritance
- Multilevel inheritance
- Multiple inheritance
- Hierarchical inheritance
1. In single inheritance, one class extends one class only. In multilevel inheritance, the ladder of single inheritance increases.
2. In multiple inheritance, one class directly extends more than one class.
3. In hierarchical inheritance one class is extended by more than one class.
2. In multiple inheritance, one class directly extends more than one class.
3. In hierarchical inheritance one class is extended by more than one class.
For a fresher, it will be very confusing, no doubt. Let us go in detail of each one programmatically. Remember, to learn Java, C++ knowledge is not at all required. In fact, you learn OOPs concepts through C++ syntax in C++ and through Java syntax in Java. That is all.
1. Multilevel Inheritance
In multilevel, one-to-one ladder increases. Multiple classes are involved in inheritance, but one class extends only one. The lowermost subclass can make use of all its super classes' members. Multilevel inheritance is an indirect way of implementing multiple inheritance. Following program explains.
Now, Parrot has two super classes Bird and Aves; but extended one-to-one. Parrot can make use of the methods of Bird and Aves. Bird can make use of the methods of Aves, but Parrot as a super class cannot access subclass members. Following figure gives a schematic representation of the multilevel hierarchy with the classes involved in the program.
2. Multiple Inheritance
In multiple inheritance, one class extends multiple classes. Java does not support multiple inheritance but C++ supports. The above program can be modified to illustrate multiple inheritance. The following program does not work.
In the above code, Parrot extends both Aves and Bird. This is not supported by Java and the above code raises compilation error. Following is the schematic representation.
3. Hierarchical Inheritance
In hierarchical type of inheritance, one class is extended by many subclasses. It is one-to-many relationship. A realtime example is available at dynamic binding.
In the above code, Aves class is extended by two classes – Parrot and Vulture. Both classes can make use of the methods of Aves. Even though the Parrot and Vulture are subclasses of the same class Aves, but still they cannot make use of each other members. Parrot and Vulture are known as "siblings". Siblings are disjoint and they cannot make use of other members as between them no inheritance is involved (like two sons of a father; one son's property cannot be shared by other but both can share the property of father). Following is the schematic representation of the classes involved.
Dynamic binding and dynamic polymorphism use hierarchical inheritance.
Disadvantages of Inheritance
- Both classes (super and subclasses) are tightly-coupled.
- As they are tightly coupled (binded each other strongly with extends keyword), they cannot work independently of each other.
- Changing the code in super class method also affects the subclass functionality.
- If super class method is deleted, the code may not work as subclass may call the super class method with super keyword. Now subclass method behaves independently.
Abstract Methods and Classes
An abstract class is a class that is declared
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
If a class includes abstract methods, the class itself must be declared
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared
abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
abstract, as in:public abstract class GraphicObject {
// declare fields
// declare non-abstract methods
abstract void draw();
}
abstract.Interfaces
There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, interfaces are such contracts.
For example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software (Java, of course) that operates the automobile—stop, start, accelerate, turn left, and so forth. Another industrial group, electronic guidance instrument manufacturers, make computer systems that receive GPS (Global Positioning System) position data and wireless transmission of traffic conditions and use that information to drive the car.
The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group's software is implemented. In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface.
For example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software (Java, of course) that operates the automobile—stop, start, accelerate, turn left, and so forth. Another industrial group, electronic guidance instrument manufacturers, make computer systems that receive GPS (Global Positioning System) position data and wireless transmission of traffic conditions and use that information to drive the car.
The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group's software is implemented. In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface.
No comments:
Post a Comment