Chapter
5: Overloading overriding runtime type and Object Orientation
- The identity of a method is determined
by the combination of its fully qualified class, name and the type,
order and count of arguments in the argument list, this is often
known as the signature of this method
- Overloading is reusing the same method
name with different arguments and perhaps a different return type.
- Overloaded methods aren't required to
have the same return type or the list of thrown exceptions.
- Overloading is determined at runtime
- Overloading is particularly useful when
you will be creating several methods that perform closely related
functions under different conditions
- Methods with overloading names are effectively
independent methods
- overloaded methods can call one another
simply by providing a normal method call with an appropriately formed
argument list.
- Constructors can also be overloaded after
all they are also methods.
- there are some classes in jfc which have
more than one constructors. ex :java.lang.String class provides
11 different constructors.
- One constructor can call another consructor
in the same class by using the "this" keyword.
Method overriding:
When a class defines a method with same name, return type and arguments
as a method in its super class, the method in the class is said to
be override the method in the super class.
- Overriding modifies the implementation
of a particular piece of behaviour for a subclass.
- Methods overriding can't be declared
more private than the superclass method.
- Any exceptions declared must be the same
class as that thrown by the super class,or a subclass of that type.
- The subclass method must have the same
return type as the super class method.
- Methods declared final cannot be overriden.
- Methods declared private cannot be overriden
as they are visible outside the class.
- A method declared public can be overriden
by declaring it final.
- Each method in a parent class can be
overriden only once in any one class.
- Overloaded methods supplements eachother,
while an overriding method replaces the method it overrides.
- Overriding methods must have arguments
lists of identical type and order.
- The overriding method must not throw
checked exceptions of classes that are not possible for the original
method.
- Overriding is determined at runtime.
- super.somemethod() is a call to the method
of immediate super class.
- super.super.somemethod() is illegal.
- A subclass can have a variable with the
same name as a variable in the parent class.
- The above is shadowing the parent class
variable not overriding.
"Is a" and "has
a" relationhip: This is a basic difference whether an
object should be implemented using inheritance (is a) or should be
implemented using containment(has a).
- inheritance(is a) is for a specialization
of a type.
- container (has a) class are for a code
reuse.
Encapsulation:
This is seperating the interface of a class from its implementation.This
means you can't accidently corrupt the value of a field, you have a
method to change a value.
- Encapsulation involves hiding data of
a class and allowing access only through a public interface.
- The standard naming convention for the
method which access the private variables.
- setFieldName()
- getFieldName()
- The seperation of interface and implementation
makes it easier to modify the code within a class without breaking
any other code that uses it.
- Due to encapsulation the enduser of the
class donot have to understand how the internals work.
- The end user will be confident, that
updates to the class code will not break thier existing code.
instanceof operator:
instanceof operator evaluates true if some object is an instanceof
same class or is an instanceof a subclass of same class,otherwise
it evaluates as false.
ex:some object instanceof some class.
- also instanceof evaluates as true if
some object is an instanceof a class which implements some Interface.
- Consider the following example
-
class Base{
-
int X=99;
System.out.println("Base.amethod()");
}
}
public class Rtype extends
Base{
int X=-1;
public static void main(String
args()){
Base b=new Rtype();
System.out.println(b.X);
b.amethod();
}
public void amethod(){
System.out.println("Rtype.amethod");
}
}
The type of reference is b Base but the type
of actual class is Rtype.
The call to a method will invoke the version
in Rtype but the call to output b.X will reference the field X in the
Base class.
- The above happening is because the JVM
performs a lookup based on the actual class of the object.
- The X variable that is accessed is the
shadowed variable in Base.
- This rather surprising difference is due
to the fact that the address used to access X was calculated at compile
time.
- The compiler computes access to variables
based on the reference type in the expression.
Inner Classes:
class Base{
class innerclass{
}
}
- when the above class is compiled we get
two class files,
1] Base.class
2] Base$innerclass.class
- If you define an inner class at the same
level as the enclosing class instance variables,the inner class can
access those instance variables, no matter what their access control.
- If you define an inner class within a method,
the inner class can access the enclosing class instance variables
and also the local variables and parameters for that method.
- An inner class cannot have the same name
as its enclosing class.
- An inner class can extend any class or
interface.
- You can create four different types of
inner classes:
- Nested top-level classes
- Member classes
- Local classes
- Anonymous classes
detailed explanation of inner classes will
be added soon
|
Copyrights anilbachi.8m.com
All rights reserved
for queries contact anil_kuchana@yahoo.com
|
|