Put your text ad here
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
Top Canadian Hotels no booking fees from Victoria BC to Nova Scotia
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.
Object inheritance create multiple inheritance?
Object class?
void instead of an object?
A: The authors of the Java language took a design decision to compromise multiple inheritance with interfaces, the specifics of this decision may be covered in other sources. Practically, multiple inheritance is difficult because it creates ambiguity when a class inherits methods from two superclasses with the same method signature: which version should be called?
… premium content omitted
Access all premium content for $50: sign-up now.
Why doesn't Java support multiple inheritance?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
Object inheritance create multiple inheritance?
A: No, this is a misunderstanding of implicit object inheritance. Every Java class is ultimately descended from the Object superclass, whether there is an extends Object statement or not. If you extend an API class, the Object inheritance is passed down through that immediate superclass, not from the superclass and the Object class too. Implicit inheritance from the Object class descends through a single line of parent classes to ensure all user defined classes have the standard toString(), equals() and hashCode() methods.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: You should make a judgement about which forms of inheritance are better for a particular Java application. In many cases you should use a mixture of the two forms of inheritance. The judgement you make will depend on the nature, scale and context of the solution you are developing.
For most relatively simple, small scale applications that can operate independently of other Java systems, class inheritance works well because of its simplicity and re-use of code. Interface inheritance tends to be used in slightly larger, broader applications, but is usually used in combination with class inheritance, not separately.
Some Java programmers argue that class inheritance should be avoided because it creates a rigid coupling and dependency between a class and its superclasses. A class can only have one superclass, so it can be difficult to extract a class from its hierarchy for use elsewhere. A more flexible alternative is to compose a class of one or more internal class fields and fulfil an interface by creating a thin “wrapper” around them.
The example below implements a List and a Named interface by adapting a String and a Vector, not by inheritance. The String and the Vector are encapsulated by the host class so it is not tightly coupled.
… premium content omitted
Access all premium content for $50: sign-up now.
Is multiple interface inheritance better than extending a class?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The short answer to your question is no, this is one of the limitations of the Java language. However, if you use object composition, it can be relatively easy to fulfil two or more interfaces with adaptor code that passes calls through to the underlying objects.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: One of the key benefits of inheritance is to minimise the amount of duplicate code in an application by putting common code in a superclass and sharing amongst several subclasses. Where equivalent code exists in two related classes, the hierarchy can usually be refactored to move the common code up to a mutual superclass. This also tends to result in a better organisation of code and smaller, simpler compilation units.
Inheritance can also make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably. If the return type of a method is superclass Example, then the application can be adapted to return any class that is descended from Example.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: There are two forms of inheritance in the Java language. The standard form of inheritance is by extension; a class declares that it extends another class, or an interface extends another interface. In this case, the subclass or sub-interface inherits all the fields and methods of its parent.
The second special form of inheritance is where classes declare that they implement an interface, which has more limited consequences. When a class implements an interface, it inherits any fields from the parent as final constants, but must provide its own implementation of the interface methods.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: An example of inheritance using the extends keyword would be the java.util.Stack class that extends the Vector class to provide a last-in-first-out stack of Objects. The Stack class uses the same underlying storage facilities provided by the superclass and adds its own, so it “is a” type of Vector and behaves like a specialised “wrapper” around the Vector class. The Stack class adds a boolean empty() method, peek(), pop() and push() methods to manage the stack contents and a search() method to find the index of a given object. All the original methods of the Vector class remain and the Stack can be cast to a Vector as necessary.
… premium content omitted
Access all premium content for $50: sign-up now.
Can you show examples of Java inheritance?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Specialization is an application of inheritance, not a particular form of inheritance. From a technical point of view one can only apply the extends and implements keywords in Java to achieve inheritance; the way that a programmer implements the inheritance scheme affects the nature of super- and sub-class relationships. For example, abstract classes and interfaces are typically used to specify the methods of their subclasses, which is specification. Subclasses often provide special versions of the more general superclass, that is specialization.
There are no Java keywords to declare particular applications of inheritance in Java, they are formed by a combination of extends, implements and other Java programming devices like abstract and final methods, visibility modifiers and so on. Really it is the overall configuration of class, variable and method hierarchies that shapes the inheritance relationships that exist, and classes may serve more than one role.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Inheritance is a design principle in object oriented languages like Java. Inheritance means that classes acquire methods and properties by declaring that they are a sub-class of a class that already has those features. This can significantly improve the efficiency and management of code because some methods only need to be written once and can be used by any number of sub-classes.
An abstract class is one that is designed to provide methods or properties to sub-classes like a template. Abstract classes cannot be instantiated in their own right and usually contain abstract methods that sub-classes must implement to complete the intended inheritance program design. For instance, abstract methods may have a concrete method that calls an abstract method, whose implementation varies in each sub-class.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: To show how Java inheritance can avoid duplication, consider two classes: Car and Motorcycle which both implement an interface method moveForward(). Here's some example code for the moveForward() method in the Car class.
… premium content omitted
Access all premium content for $50: sign-up now.
How does inheritance avoid duplication?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: When a Java class is imported, the type it represents is made available to the host class to use as if it were contained within the host. The imported class is not “visible” through the public interface of the host unless it is declared to extend it.
… premium content omitted
Access all premium content for $50: sign-up now.
What's the difference between importing and extending a class?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
Object class?
A: Whenever we declare a new class without an explicit extends keyword, the class implicitly extends the fundamental Object class. When you explicitly extend a different class, that will be descended from the Object class at some point in its hierarchy. It may help to understand the inheritance structure more clearly if you explicitly extend the Object class and it would not be an error to do so.
public class Example1 { // Implicitly extends Object } public class Example2 extends Object { // Explicitly extends Object } public class Example3 extends Example1 { // Ultimately extends Object }
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Java inheritance has been designed to be universal; any non-final Java class can be extended without any special handling for graphical user interface types. Child classes will directly inherit all non-private fields and methods of the parent, which can be overridden and extended however you choose.
Having said that, graphical user interface classes can be quite complex and rely upon specific method calls to maintain their integrity. Be cautious if you override methods and make sure you check there are no unintended side effects of your changes. If you are not certain of the consequences of your overrides, call the overridden superclass method before your own method statements.
In general, it is safest only to add features to GUI classes or build new structures by composition.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: To prevent a class from being extended or subclassed include the final modifier in the class declaration statement, as below.
public final class FinalClass { // Class definition }
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: So long as a class and its methods are not marked final, a subclass can declare a method with the same signature as one in a superclass and its own implementation. A typical example is the String toString() method, which is declared in the Object class. All objects implicitly inherit this method. The default implementation is to output the class name combined with its hash code in hexadecimal. The String class overrides the toString() method to give a Unicode representation of string content of the object. Any other object may override toString() with its own method body, to return its own custom output.
public String toString() { return "All classes may override non-final methods."; }
Actions: Follow-up, clarify or correct this answer. Submit a new question.
void instead of an object?
A: It will often help to understand the Java language if you create small test classes, compile and run them. If you try to compile this test case, you will find it fails because the method signatures have different return types: void and an object reference. In Java, void is a return type and void methods must not return object references.
To override a method, the subclass method must have the same signature as the superclass: return type, method name and arguments. If the return type of the subclass method is different it will fail to compile, “attempting to use incompatible return type”.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: An overridden method in a subclass must take priority over the superclass implementation because a class can only expose one implementation of a particular method signature through its public API. The technique of overriding effectively says “use this special implementation of the method instead of the superclass version”.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The method override example below shows several aspects of inheritance. Both classes implement the ObjectRenderer interface, which has one method, void render(Object). The superclass BasicObjectRenderer has a default constructor and uses the System.out PrintStream to render the object, the subclass HTMLObjectRenderer has a constructor that takes an OutputStream argument to render the object with HTML formatting.
… premium content omitted
Access all premium content for $50: sign-up now.
Can you give an example application that shows overriding?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: To prevent a specific method from being overridden in a subclass, use the final modifier on the method declaration, which means “this is the final implementation of this method”, the end of its inheritance hierarchy.
public final void exampleMethod() { // Method statements }
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: It is possible to deduce the inherited fields and methods of a Java class using reflection through a recursive process that uses the Class method getSuperclass(). The Class method getFields() returns an array of Field objects for public fields only. The getDeclaredFields() returns an array of all declared fields in the current class, including private fields, but not the inherited fields. The getMethods() and getDeclaredMethods() methods return equivalent arrays of Method objects.
To obtain a list of all inherited fields or methods, a program would need to get the superclass of the object in question, store the return value for its getDeclaredFields() method then process each of its superclasses in turn. The simple example below prints out all the inherited fields in its immediate superclass Thread.
… premium content omitted
Access all premium content for $50: sign-up now.
How can I find the inherited attributes of a class?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: To call a superclass method that has been overridden in a subclass, you must either call the method directly through a superclass instance, or use the super prefix in the subclass itself. From the point of the view of the subclass, the super prefix provides an explicit reference to the superclass' implementation of the method.
// From subclass
super.overriddenMethod();
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The example below shows a general class GeneralNotifier with a method printMessage() which is overridden in the subclass SpecialNotifier. The SpecialNotifier also has a method printParentMessage() that calls the superclass version of printMessage().
… premium content omitted
Access all premium content for $50: sign-up now.
Can you give an example call to an overridden superclass method?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: This type of call to a method in the superclass chained to a call in the subclass can only be done if the superclass calls an abstract method that is overridden in the subclass, see the example below.
… premium content omitted
Access all premium content for $50: sign-up now.
How can I get the superclass to call back to a method in the subclass?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
About us: site help, text ads and premium content FAQs.