Next: 1.12 The Visitor Pattern Up: 1.11 Loose Ends Previous: 1.11.3 Exceptions as Errors In Java, the same name can simultaneously be used for a local variable or method parameter, different fields of the same object, and different methods of the same object. Java uses context and type information to determine the meaning of a name. For example, many Java programmers would write the constructor for the class Cons above as follows: In this example, the names first and rest each have two different meanings. Java resolves potentially ambiguous uses of names by using the innermost (most local) definition of the name. Hence, the prefix this. is required to distinguish the first and rest fields of this from the constructor parameters of the same name.Cons(Object first, FunList rest) { this.first = first; this.rest = rest; } While the use of the same name for different kinds of program entities is widely accepted as good programming practice, the use of one name for several different fields or methods is more controversial. A Java subclass B can introduce a field with exactly the same name n as a field in its superclass A. The inherited field is not overridden (what would overriding mean in this context?); it is merely ``shadowed''. When the name n appears in a method of B, its meaning depends on the type of the receiver. If the receiver is this, then the new field n introduced in B is meant. But if the receiver has type A rather than B, then the old field n introduced in A is meant. Warning: duplicate field names can be the source of insidious program errors that are very difficult to diagnose. For this reason, we strongly recommend against using them. Show
Duplicate method names are less controversial but can still be dangerous. In a class, Java permits the same method name to be used for different methods as long as their argument lists do not identical the same length and same types. The practice of defining more than one method in a class with same name is called method overloading. Java resolves overloaded method names using the types of the argument expressions. When the Java compiler encounters a method invocation involving an overloaded method, it determines the types of the method arguments and uses this information to select the ``best'' (most specific) match from among the alternatives. If no best method exists, the program is ill-formed and will be rejected by the Java compiler. We urge restraint in using the same name for different methods involving the same number of arguments. Since static type information is used to resolve which method is meant, program errors may be difficult to find because the programmer may not infer the correct static type when reading the code. Next: 1.12 The Visitor Pattern Up: 1.11 Loose Ends Previous: 1.11.3 Exceptions as Errors Corky Cartwright 2000-01-07 Given below is an extract from the -- Java Tutorial, with slight adaptations. 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). 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. Every class has one and only one direct superclass (single inheritance), except the
The The keyword Here is the sample code for a possible implementation of a
A subclass inherits all the fields and methods of the superclass. In the example above, Accessing superclass membersIf your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword Consider this class,
Within
Subclass constructorsA
subclass constructor can invoke the superclass constructor. Invocation of a superclass constructor must be the first line in the subclass constructor. The syntax for calling a superclass constructor is The following example illustrates how to use the
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 superclass does not have a no-argument constructor,
you will get a compile-time error. Access modifiers (simplified)Access level modifiers determine whether other classes can use a particular field or invoke a particular method. Given below is a simplified version of Java access modifiers, assuming you have not yet started placing your classes in different packages i.e., all classes are placed in the root level. A full explanation of access modifiers is given in a later topic. There are two levels of access control:
When 2 or more methods in the same class have the same name it is called method?When two or more methods in the same class have the same name but different parameters, it's called overloading.
When you create two or more methods with the same name but they have a different parameter lists this is called method overloading?Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters. When a method is called, the corresponding method is invoked by matching the arguments in the call to the parameter lists of the methods.
Can a class have two methods with the same name?Yes, we can define multiple methods in a class with the same name but with different types of parameters.
What is it called when methods are used with the same name?The practice of defining more than one method in a class with same name is called method overloading.
|