MY mENU


Monday 12 March 2012

Methods in Java


Methods:

  • If you define more than one method with the same name in the same class, Java must be able to determine which method to invoke based on the number and types of parameters defined for that method.
  • The compiler will complain if you have two methods with identical signatures exception for the return type and/or the exceptions thrown, i.e. can’t overload based on return type and/or exceptions thrown.
  • Determining which methods will be invoked with integer params.
  • It is possible to declare an inherited method abstract.
  • Obviously, it is not possible to override a final method.
  • A subclass may make an inherited method synchronized, or it may leave off the synchronized keyword so that its version is not synchronized. If a method in a subclass is not synchronized but the method in the superclass is, the thread obtains the monitor for the object when it enters the superclass’s method.
  • It is possible to declare an inherited method as abstract, but then there is no way to get to the behaviour in the hierarchy above the abstract declaration.
  • Return types must match the overriden method in the superclass exactly. The parameter types must match those in the superclass exactly, i.e. in the same order. If this is not the case, then the superclass’s method is not overridden. Compiler will not complain, however, unless exactly the same signature except for return type (or exceptions thrown).
  • You cannot make a method in a subclass more private than it is defined in the superclass, ‘though you can make it more public.
  • Coercion of arguments only happens with overloading, not overriding.
  • It is perfectly legal to have two different instance variables with the same name if they are defined in different classes where one class inherits from the other.
  • Which variable is accessed depends on the type of object reference which the variable was declared to hold. Which method gets invoked depends on the underlying object. 
  • A native method does not have a body, or even a set of braces, e.g. public native void method();
  • A native method cannot be abstract.
  • A native method can throw exceptions.

No comments:

Post a Comment