Inheritance Flashcards
class Dog extends Animal
Dog -is a subclass of class Animal
*A subclass can ALWAYS be assigned to a super class variable without any cast. It will always compile and run without any exception.
* but an Animal is not always a DOG, that is way for the reverse you need a CAST for it to compile
* and if at runtime the actual obj referenced by Animal is not a Dog then a ClassCastException will be thrown
C1 and C2 extends B1
B1 and B2 Extends A
Assume that method public void m1(){ … } is defined in all of these classes EXCEPT B1 and C1.
*C1 will inherit B1’s m1() which in turn inherits m1() from A.
*C2 has m1(), so its m1() will override A’s m1().
What You Can Do in a Subclass
A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:
The inherited fields can be used directly, just like any other fields.
You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
You can declare new fields in the subclass that are not in the superclass.
The inherited methods can be used directly as they are.
You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
You can declare new methods in the subclass that are not in the superclass.
You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.
Private Members in a Superclass
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
Casting Objects
You can assign a subclass object reference to superclass reference without a cast but to assign a super class object reference to a subclass (or interface) reference you need an explicit cast !!!!!!!!!!
Superclass a = Subclass b;
Subclass b =(SubClass) Superclass a;
Implicit casting:
Object obj = new MountainBike();
Explicit casting:
MountainBike myBike = (MountainBike)obj;
–>This cast inserts a runtime check that obj is assigned a MountainBike so that the compiler can safely assume that obj is a MountainBike. If obj is not a MountainBike at runtime, an exception will be thrown.You can make a logical test as to the type of a particular object using the instanceof operator. This can save you from a runtime error owing to an improper cast
Multiple Inheritance of State, Implementation, and Type
1.NOT ALLOWED in java : Multiple inheritance of state –> ability to inherit fields from multiple classes (I guess because when an object will inherit fields from all of the class’s superclasses, it is ambiguous what methods or constructor to use to instantiate/modify the state/ the fields??? )
2.Multiple inheritance of implementation–> the ability to inherit method definitions from multiple classes with DEFAULT METHODS!!!!!!!!!
Problems arise with this type of multiple inheritance, such as name conflicts and ambiguity. The Java compiler provides some rules to determine which default method a particular class uses:
**Instance methods are preferred over interface default methods!!!!
**If two or more independently defined default methods conflict, or a default method conflicts with an abstract method, then the Java compiler produces a compiler error. You must explicitly override the supertype methods.ou could invoke any of the of the default implementations with the super keyword.
**Methods that are already overridden by other candidates are ignored. This circumstance can arise when supertypes share a common ancestor.
EG: the overring method has precedence!
public interface Animal {
default public String identifyMyself() {
return “I am an animal.”;
}
}
public interface EggLayer extends Animal {
default public String identifyMyself() {
return “I am able to lay eggs.”;
}
}
public interface FireBreather extends Animal { }
public class Dragon implements EggLayer, FireBreather {
public static void main (String… args) {
Dragon myApp = new Dragon();
System.out.println(myApp.identifyMyself());
}
}
The method Dragon.identifyMyself returns the string I am able to lay eggs.
3.Multiple inheritance of type–> the ability of a class to IMPLEMENT more than one INTERFACE.
An object can have multiple types: the type of its own class and the types of all the interfaces that the class implements. This means that if a variable is declared to be the type of an interface, then its value can reference any object that is instantiated from any class that implements the interface. a class can inherit different implementations of a method defined (as default or static) in the interfaces that it extends. In this case, the compiler or the user must decide which one to use.???????
STATIC Methods in interfaces
The idea behind static interface methods is to provide a simple mechanism that allows us to increase the degree of cohesion of a design by putting together related methods in one single place without having to create an object.
The same can pretty much be done with abstract classes. The main difference is that abstract classes can have constructors, state, and behavior.
Furthermore, static methods in interfaces make it possible to group related utility methods, without having to create artificial utility classes that are simply placeholders for static methods.
Overriding and Hiding Methods
Static methods and fileds(static or non-static ) are never overridden –> they are hidden!!!!!!!!!!!!!!
An INSTANCE method in a subclass with the SAME SIGNATURE(name, plus the number, the order? and the type of its parameters) and return type(or a subtype=covariant) as an instance method in the superclass OVERRIDES the superclass’s method.
If a subclass defines a STATIC method with the same signature as a static method in the superclass, then the method in the subclass HIDES the one in the superclass
The distinction between hiding a static method and overriding an instance method has important implications:
The version of the overridden instance method that gets invoked is the one in the subclass.
The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.
EG:
class A{
int i = 10;
public static void m1(){ }
public void m2() { }
}
class B extends A{
int i = 20;
public static void m1() { }
public void m2() { }
}
Here, UNLIKE m2, m1() of B does not override m1() of A, it just hides m1() of A, as proven by the following code:
A a = new B();
System.out.println(a.i) //will print 10 instead of 20
a.m1(); //will call A’s m1
a.m2(); //will call B’s m2 as m2() is not static and so overrides A’s m2()
Note: Static methods in interfaces are never inherited.
whaaaat???
Modifiers
The access specifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the superclass can be made public, but not private, in the subclass.
You will get a compile-time error if you attempt to change an instance method in the superclass to a static method in the subclass, and vice versa.
Defining a Method with the Same Signature as a Superclass’s Method
Defining a Method with the Same Signature as a Superclass’s Method
A subclass INSTANCE methods
–>overrides a superclass instance method
–> generated copile time error for a Superclass static methods
A subclass STATIC methods
–>generated compile-time error for a superclass instance method
–> hides a Superclass static methods
EG:
void methodA(){
System.out.println(“base - MethodA”);
}
}
class Sub extends Base{
public void methodA(){
System.out.println(“sub - MethodA”);
}
public void methodB(){
System.out.println(“sub - MethodB”);
}
public static void main(String args[]){
Base b=new Sub(); //1
b.methodA(); //2
b.methodB(); //3
}
}
–> genereated Compile time error at //3 because methodB() is not defined in Base.
The point to understand here is, b is declared to be a reference of class Base and methodB() is not defined in Base. So the compiler cannot accept the statement b.methodB() because it only verifies the validity of a call by looking at the declared class of the reference.
For example, the compiler is able to verify that b.methodA() is a valid call because class Base has method methodA. But it does not “bind” the call. Call binding is done at runtime by the jvm and the jvm looks for the actual class of object referenced by the variable before invoking the method.
Overloading
In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass instance methods—they are new methods, unique to the subclass.
Polymorphism
The JVM calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable’s type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.
Hiding Fields
NOT recommended.
Within a class, a field that has the same name as a field in the superclass hides the superclass’s field, even if their types are different. Within the subclass, the field in the superclass cannot be referenced by its simple name. Instead, the field must be accessed through super,
Super keyword
Invocation of a superclass constructor must be the first line in the subclass constructor.
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 super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.