Java-Inheritance and Interface

  • sub-class inherit public and protected variables and methods from super-class
    • exp. car(super)-suv(sub)
    • although it still contain private function and variables, user can not use it in sub class
      • compiler error
    • the sub-class can add new variable and methods or override exist method from super-class
      • overwrite a method, and compiler will choose function base on object type
        • this is called gynamic method lookup
          • allow us to treat object of different class in a uniform way
        • this  feaure is called polymorphism
          • make program extensible
      • variable can not been override
      • use super.accessor()  / super.mutator()  to get/change private variable value
      • use super() to call constructor of super class(use in sub-constructor, must be first line)
    • sub class data can convert to super-class data
      • if A is super class, B is sub class of A
      • A a = new B() create a B type variable
      • A a = (A) b convert a B type variable to A type
  • abstract class
    • a class with abstract methods can work as super class to many class
    • basically contain common ideas of all sub-class
    • if extend abstrat class, must implement all abstract methods
    • you can not instantiate an abstract calss
      • because abstract class do not have all property the real object does
      • means you can not create a object with abstruct class
  • you can prevent others over write method/variable using final
  • tostring method
    • return a string contain all info of current object
    • all class have such method because all class extend object class
    • can inherit by sub class and use super.toString()+… to overwrite(save time)
  • the == is different to .equals()methods
    • all class have such method because all class extend object class
  • instanceof operator
    • check if object is an data of given type
      • (obj instanceof class)
    • return a boolean value
    • do not use type test to seperate case of super and sub class
      • write a method in super class, overwrite in sub class with different task
  • use getClass method to get and compare object`s class

 

 

  • Interface
    • a list of methods and their signatures
      • these methods just have name, parameter and return type, but not decleared
      • atomatic public
      • do not contain instance variable/static method
        • instance variable: variable decleared outside class
        • static method: method do not apply to object
    • a class implements a Interface must have all methods contained by that Interface
      • all implement method must be public
    • <T> a placeholder for method is some interface to make sure these method can imply to any  data type
    • exp.
      • measureable interface
        • getMeasure() method
      • comparable interface
        • compareTo() method
    • can have variable which is public static final
      • not changable but can be access by any class implement to it

留下评论