Put your text ad here
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
25% off cpanel web hosting and reseller hosting deals. Promo: codestyle25off
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs and more.
Get the latest answers in this FAQ
Follow the Code Style Twitter feed for free one-to-one help sessions by instant messenger: MSN, AIM and Yahoo! Messenger. Sessions are held most Saturdays. Join the Twitter feed then check your direct messages for details.
Object inheritance create multiple inheritance?
void instead of an object?
protected modifier for?
protected methods don't have to call super?
protected method?
A: One of the key benefits of inheritance is to minimise the amount of duplicate code in an application by sharing common code 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 sub-class 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: 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 types of the more generalised 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 implementation of class hierarchies that shapes the inheritance relationships that may exist and classes may serve more than one role.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
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 of the ambiguities it can create when a class inherits from two superclasses with the same method signature: which version should be called?
More details available to premium content service subscribers:
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: 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: 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 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: 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.
More details available to premium content service subscribers:
What's the difference between importing and extending a class?
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 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 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: 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.
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.
More details available to premium content service subscribers:
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.
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.
More details available to premium content service subscribers:
How can I find the inherited attributes of a class?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Declaring a class final is the simplest way to prevent extension, it is not clear why you would want to do it some other way. A more limited way to control overrides without preventing extension is to declare individual methods final.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The Java visibility modifiers are firstly concerned with which classes can access the variables and methods, and secondly with inheritance. If a field or method is marked public or protected they will be accessible in sub-classes too. If they have no explicit visibility modifier, they have an implicit package visibility status, which is accessible to sub-classes in the same package, but not to sub-classes in a different package.
The private visibility keyword means only accessible from the host class, public means any class can read a variable or call a method.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
protected modifier for?
A: The Java visibility modifiers are concerned with data hiding and encapsulation, they control which classes and sub-classes can read and write variable values and call methods. The protected visibility modifier means that the field or method is accessible from within the host class itself, and internally to other classes in the same package and subclasses that may be in a different package. Protected fields cannot be accessed through the public API of a class.
The protected modifier is usually used to develop the features of an inheritance hierarchy by sharing access to common variables and methods with subclasses. It offers greater scope for extension than package visibility without creating a public API.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
protected methods don't have to call super?
A: The use of the protected modifier on a superclass method means that any subclass can call that method on the superclass through itself. Such calls are made with an implied "this" reference with the same syntax as a call to one of their own instance methods, they do not require an explicit super. prefix.
int example = protectedGetInt();
In this respect protected method inheritance is the same as public method inheritance, but the protected modifier means that the method is not part of the class' public API; other classes cannot call the method on an instance of the class. Typically another class may call a public method that internally calls the protected method to complete the request.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
protected method?
A: With this type of question it is often easiest to create a minimal test case and attempt to compile it. In this case you will find that the compiler will fail and issue a warning if you attempt to assign "weaker" access privileges to the overridden methods using private or implicit package private modifiers. The term weaker is slightly misleading in this context; it means that the private and package access modifiers are more restrictive than the parent classes' method, which is not permitted. In short, only protected or public visibility modifiers can be used to override protected methods.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Yes, Java class constructors are assigned access modifiers. If none is explicitly declared, the constructor will implicitly have package visibility, which means it is accessible by the class itself and other classes in the same package only. Constructors may also be assigned private, protected or public access. The choice of constructor visibility modifiers will depend on the nature of your application, but the notes below give some broad principles in each case.
More details available to premium content service subscribers:
Can constructors have visibility modifiers?
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, sponsored links and premium content FAQs.