Java Inheritance
What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these members as is, or it can hide the member variables or override the methods.
A little more detail Subclasses inherit those superclass members declared as public or protected. Subclasses inherit those superclass members declared with no access specifier as long as the subclass is in the same package as the superclass. Subclasses don't inherit a superclass's member if the subclass declares a member with the same name. In the case of member variables, the member variable in the subclass hides the one in the superclass. In the case of methods, the method in the subclass overrides the one in the superclass.
Overriding Methods The ability of a subclass to override a method in its superclass allows a class to inherit from a superclass whose behavior is "close enough" and then override methods as needed. For example, all classes are descendents of the Object class. Object contains the toString method, which returns a String object containing the name of the object's class and its hash code. Most, if not all, classes will want to override this method and print out something meaningful for that class.
Calling the Overridden Method Sometimes, you don't want to completely override a method. Rather, you want to add more functionality to it. To do this, simply call the overridden method using the super keyword. For example, super.overriddenMethodName();
Methods a Subclass Cannot Override A subclass cannot override methods that are declared final in the superclass (by definition, final methods cannot be overridden). If you attempt to override a final method, the compiler displays an error message similar to the following and refuses to compile the program:
Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass.
Methods a Subclass Must Override A subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract
Overriding vs Overloading Overriding a method from a base class in a subclass allows the behavior to differ between the classes. In Base printName(){ System.out.println( “Name is “ + name): } In Subclass printName(){ System.out.println(“My name is “+name)
Overloading is having different parameters for the same method name such as in constructors Person() Person(String fname, String lname) Person(int age) Person(String address)
Upcasting A subclass reference can be assigned to a base class reference because…. A subclass object IS a baseclass
This and Super The reference “this” refers to the current object. When an method is called, this denotes the object in which the method is called. Super This is a call to the baseclass of an object and can be used to call methods in the baseclass from a subclass
Java Interfaces and Abstract Classes
Abstract Classes In polymorphism and inheritance some classes are so generic that they should not be instantiated. Declaring a class abstract keeps it from being instantiated. The abstract class may or may not implement all methods regardless a subclass will inherit all the members of the abstract class
References to Abstract classes if classB extends classA and class a is abstract class B still passes the “is a “ test classA = new classB() creates an instance of classB but is using a reference of classA When you call a non abstract method or an abstract method dynamic binding will determine which subclass method is being called.
Interfaces
Sharing common methods Inheritance is between objects that can pass the “is a” test. Objects that do not pass this test still may need to share methods or constants
Interfaces Interfaces are a collection of constants and methods Constants in interfaces are by default Public Static Final Methods are by default Abstract
Implementation class dog extends Animal implements Sounds{ code} To use an interface, you must provide the code for all methods in the interface. Classes may implement several interfaces. Separate by a comma
Syntax for an interface Similar to class public interface ConverstionFactors{ double Inch_to MM = 2.54; double OunceToGram = 28.342; public inchToMM(double inch); public ounceToGram(double ounce); }
Interfaces Interfaces can be extended public interface Conversion extends conversionFactors{ public double poundsToGrams(double pounds): } This will have all the constants and methods of the super-interface
What to use? Keep in mind that the core reason for interfaces is to be able to have shared members of more than one base type. However, a second reason for using interfaces is the same as using an abstract base class: to prevent the client programmer from making an object of this class and to establish that it is only an interface.
Should you use an interface or an abstract class? you should always prefer interfaces to abstract classes. In fact, if you know something is going to be a base class, your first choice should be to make it an interface, and only if you’re forced to have method definitions or member variables should you change to an abstract class, or if necessary a concrete class.