Free Guides
Language Tutorials

Index

Object-oriented programming
-
Private, overridden and hidden members of the superclass are not inherited.
-
This ability of a superclass reference to denote objects of its own class and its subclasses at runtime is called polymorphism and is carried out by dynamic method lookup. A call to a private instance method is not polymorphic since such a call can only occur within the class and gets bound to the private method implementation at compile time.
-
Overriding of Instance Methods involve defining a method in the subclass with the same method signature (which does not encompass final modifier for the parameters, but does include the return type of the method9). Note that the new method cannot narrow the accessibility but can widen it10, and also it can only specify all, none or a subset of the former's exceptions.
-
An instance method in the subclass cannot override a static method, a final method and a private method in the superclass. In the first case it will result in hiding of the static method, in the second a compile time error, and in the third it will result in the creation of a completely separate method with identical signature.
-
When an instance method is invoked on an object using a reference, it is the class of the current object denoted by the reference, not the type of reference, that determines which method is implemented.
When a field of an object is accessed using a reference, it is the type of the reference, not the class of the current object denoted by the reference, that determines which field will actually be accessed. (Treatment is just the same for static methods and variables too)
class Vehicle {
String mytype = new String(“Vehicle!”);
void sayType() {
System.out.println(“I am a vehicle”);
}
}
class Car extends Vehicle {
String mytype = new String(“Car!”);
void sayType() {
System.out.println(“I am a car”);
}
}
We are overridding the field and the method. This is how they behave :
Vehicle i = new Vehicle;
System.out.println(i.mytype); // prints- Vehicle!
i.sayType(); // prints- I am a vehicle
Car j = new Car;
System.out.println(j.mytype); // prints- Car!
j.sayType(); // prints- I am a car
Vehicle k = new Car;
System.out.println(k.mytype); // prints- Vehicle!
k.sayType(); // prints- I am a car -
Constructors cannot be inherited or overridden. They can be overloaded but only in the same class. Java requires that a this() or super() call in the constructor (if any) be the first statement in the constructor.
-
If a superclass only defines non-default constructors, then its subclasses cannot rely on implicit super() call to be inserted. This will result in compile time error.
-
A class choosing to partially implement the methods of an interface must be declared as abstract.
-
Regardless of how many interfaces are implemented, only a single implementation is provided for a member even if it has multiple declarations in the various interfaces.
-
Rules for assigning reference values (same rules apply for passing references)
A = B , where A = Destination reference, B = Source reference-
B is a class type
A is superclass of B
A is interface type that B implements -
B is interface type
A is Object
A is superinterface of B -
B is an array type
A is Object
A is array type, where element type of B is assignable to element type of A
-
-
Casting of References checks that the reference of the source is assignable to the reference of the destination. The compile time check determines if both the references can denote objects of a reference type that is a common subtype of both. At runtime it is the type of the actual object denoted by the source reference that determines the outcome of the operation. Hence a ClassCastException may result.
-
References of an interface type can denote objects of classes that implement this interface.