Computer Science – Polymorphism and Inheritance

 
 

Inheritance

  • Reusable code, maintain the code easier
  • Implemented by using 'extend' keyword
  • Hierarchy of classes
    • Parent, grandparent, ancestor (super)
    • Child, grandchild, descendant
    • Inheritance uses parent etc
    • Models a IS A relationship
    • Child class inherits the methods defined in parent class
    • Private data fields in parent class are not accessible by name only
      • Need getters and setters
    • Private methods are NOT available
    • Methods in child class OVERRIDES same method in parent class with same name
    • Overriding
      • Child class has same method with same signature as method in Parent class
        • Same name
        • Same type
        • Same return, order of parameters, just all same
      • If same method in class B and same class with same parameters in class A, then the class B method is run!
        • Unless a SUPER call is put inside the class B method
      • To prevent a override -
        • Use FINAL in parent class
      • Rules on Inheritance
        • Child classes may NOT remove attributes (instance fields) or methods
        • Public parent methods cannot override private child class methods
        • Multiple inheritance not supported in Java
        • Parent (base) class cannot inherit from child class
        • Every class automatically inherits from OBJECT
      • Derived Classes can
        • Inherit method
        • Override method
          • Supply a different public implementation of a method that exists in the base class
        • Overload method
          • Supply a method with the same name as method that exists in the base class BUT change the signature!
        • Add method
      • Derived classes can
        • Inherit field: all friends from superclass are automatically inherited, even though they might not be visible
        • Add field: supply new field that does not exist in base class

           
           

           
           

You can leave a response, or trackback from your own site.

0 Response to "Computer Science – Polymorphism and Inheritance"

Post a Comment

Powered by Blogger