Sunday, September 14, 2014

Inheritance, polymorphism, abstract class and interface (Notes of reading Head First Java)

Inheritance
  • subclass inherits from superclass by using keyword extends
    • Ex: public class subclass extends superclass {}
  • A subclass inherits all public instance variables and methods of superclass, but does not inherit the private instance variables and methods
  • A nonpublic class can only be inherited by classes from the same package
  • Inherited methods can be overridden. The method cannot be overridden if it’s marked with final modifier
  • To invoke the superclass version of a method from a subclass that’s overridden the method, use the super keyword
    • Ex: super.method()
Polymorphism
  • superclass obj = new subclass() //The left is called reference type. The right is called object type
  • With polymorphism, the reference type can be a superclass of the actual object type.
  • obj.method() // If method is overridden in subclass, then method of subclass is called though the reference type is superclass
  • Besides assignment, we can also have polymorphic arguments and return types.
  • Rules for overriding
    • Arguments must be the same types and return types must be compatible.
    • The method can’t be less accessible.
  • Overloading vs overriding
    • Overloading is having two methods with the same name but different argument lists
    • The return types can be different
    • You can’t change only the return type
    • You can vary the access levels in any direction
Abstract class
  • The main reason for abstract class is that some classes just should not be instantiated
    • Ex: abstract public class superClass{}
  • The abstract method means the method must be overridden.
    • Ex: public abstract void method(); // There is no body for abstract method
  • Abstract class can contain both abstract and non-abstract methods
  • The first concrete class in the inheritance tree must implement all abstract methods
  • You cannot make a new instance of an abstract type, but you can make an array object declared to hold that type
  • Ex:
    • superClass obj = new superClass() // illegal
    • superClass[] objs = new superClass[5] // legal, each element inside objs can be either superClass or subClass
  • You can call a method on an object reference only if the class of the reference type actually has the method
  • Ex: 
    • public class subClass extends superClass{}
    • There is a method METHOD which is only defined in subClass
    • superClass obj = new subClass()
    • obj.METHOD() // illegal, cannot pass compiler
    • The way to workaround is:
      • obj_cast = (subClass) obj
      • obj_cast.METHOD() //legal
    • The compiler decides whether you can call a method based on the reference type, not the actual object type. The method you are calling on a reference must be in the class of that reference type. Doesn’t matter what the actual object is.
  • If a class contains abstract methods, the class must be declared as abstract
  • Every class in Java extends class Object
Interface
  • A Java interface is like a 100% pure abstract class
  • To define an interface
    • public interface intClass{}
  • To implement an interface
    • public class subClass extends superClass implements intClass{}
  • Interface methods are implicitly public and abstract, so typing ‘public’ and ‘abstract’ is optional
  • Benefits of interface
    • If you use interfaces instead of concrete subclasses as arguments and return types, you can pass anything that implements that interface
    • A class can implement multiple interfaces
  • A class that implements an interface must implement all the methods of the interface, since all interface methods are implicitly public and abstract

No comments:

Post a Comment