Public classsometrying{ Public Static voidMain (string[] args) {NewSon (); NewSon (). Father (); }} classfather{//parameterless constructors for parent class father PublicFather () {System.out.println ("Father Construtor"); } //the father constructor of the parent class, the constructor is not a member function, it only represents the class itself, so a class can have more than one constructor PublicFather (intx) {System.out.println ("Father Construtor" +x); } /*the member function of the parent class Father is the normal function in the class, so it cannot be duplicated, and if you write one more of the following, the compiler will report the Duplicate method Father () in type Father error, because two member functions have clashed 。 public int Father () {System.out.println ("Father member function"); */ Public voidFather () {System.out.println ("Father member function"); } } //class son is the subclass that inherits the parent class father classSonextendsFather {//The parent class constructor is called in the constructor of the subclass. PublicSon () {Super(1); System.out.println ("Son construtor."); } //overriding a member function of a parent class Public voidFather () {System.out.println ("Inherited Father member function."); Super. Father (); } }
1.
Because a constructor with a parameter is explicitly declared in a class, the default constructor does not exist, but you do not have an explicit expression in the constructor of the subclass
Call the constructor of the parent class (when creating the subclass object, it will call the constructor of the parent class, this does not need to ask why), there is no explicit call
, the default constructor for the parent class is called by the virtual machine, but the default constructor for your parent class no longer exists, which is why the parent
The reason that the default constructor must be persisted in the class.
PS. Should develop good programming habits, any of our own defined classes, are explicitly coupled with the default constructor, after more in-depth study,
Will find that there are many benefits
2. Method Rewrite rules
- The argument list must be exactly the same as the overridden method;
- The return type must be exactly the same as the return type of the overridden method;
- Access permissions cannot be lower than the overridden methods in the parent class. For example, if a method of a parent class is declared public, overriding the method in a subclass cannot be declared as protected.
- A member method of a parent class can only be overridden by its subclasses.
- A method that is declared final cannot be overridden.
- A method declared as static cannot be overridden, but can be declared again.
- Subclasses and parent classes in the same package, subclasses can override all methods of the parent class, except for the methods declared private and final.
- Subclasses and parent classes are not in the same package, the subclass can only override non-final methods that are declared public and protected by the parent class.
- The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method declaration, or vice versa.
- The construction method cannot be overridden.
- If you cannot inherit a method, you cannot override this method
Reference: http://www.runoob.com/java/java-override-overload.html
http://blog.csdn.net/yuxiaohui78/article/details/43887373
Http://www.cnblogs.com/by-dream/p/3967123.html
Java---Builder