Java inheritance polymorphism (override-> override (= overwrite), overload-> overload, polymorphism-> polymorphism)

Source: Internet
Author: User

.

Package edu. javase. buffer;

Class demo1 {
Public demo1 (){
System. Out. println ("fun1 constend ");
}

Public demo1 (int I ){
System. Out. println ("fun1 int I constend:" + I );
}

// Overload (similar)
Public void fun1 (){
System. Out. println ("fun1 ");
}

Public void fun1 (int I, string s ){
System. Out. println ("fun1 int string ");
}

Public void fun1 (string S, int I ){
System. Out. println ("fun1 string int ");
}

Public int fun1 (int I ){
System. Out. println ("fun1 return int ");
Return I;
}

Public final void finalfun (){
System. Out. println ("fun1 finalfun ");
}
}

Class demo2 extends demo1 {
Public demo2 (){
Super (); // load the parent class Constructor
System. Out. println ("fun1 constend ");
}

Public demo2 (int I ){
Super (I); // load the parent class Constructor
System. Out. println ("fun2 int I constend:" + I );
}

// Overload (similar)
Public void fun2 (){
System. Out. println ("fun2 ");
}

Public void fun2 (int I, string s ){
System. Out. println ("fun2 int string ");
}

Public void fun2 (string S, int I ){
System. Out. println ("fun2 string int ");
}

Public int fun2 (int I ){
System. Out. println ("fun2 return int ");
Return I;
}

// Rewrite the inheritance relationship
// Public void fun1 (){
// System. Out. println ("2 rewrite fun1 ");
//}
}

Class demo3 extends demo2 {
Public demo3 (){
Super (); // load the parent class Constructor
System. Out. println ("fun1 constend ");
}

Public demo3 (int I ){
Super (I); // load the parent class Constructor
System. Out. println ("fun3 int I constend:" + I );
}

// Overload (similar)
Public void fun3 (){
System. Out. println ("fun3 ");
}

Public void fun3 (int I, string s ){
System. Out. println ("fun3 int string ");
}

Public void fun3 (string S, int I ){
System. Out. println ("fun3 string int ");
}

Public int fun3 (int I ){
System. Out. println ("fun3 return int ");
Return I;
}

// Rewrite the inheritance relationship
Public void fun1 (){
System. Out. println ("3 rewrite fun1 ");
}

Public void fun2 (){
System. Out. println ("3 rewrite fun2 ");
}

}

Public class Jicheng {
Public static void main (string [] ARGs ){
Demo1 d1 = new demo1 (1 );
D1.fun1 ();
System. Out. println ("~~~~~~~~~~~~~~ ");

Demo2 D2 = new demo2 (2 );
D2.fun1 (); // demo2 inherits all methods of the parent class demo1
D2.fun2 ();
System. Out. println ("~~~~~~~~~~~~~~ ");

Demo3 D3 = new demo3 (3 );
D3.fun1 (); // demo2 inherits all methods of the parent class demo1 and super parent classes.
D3.fun2 ();
D3.fun3 ();
System. Out. println ("~~~~~~~~~~~~~~ ");

Demo1 D11 = new demo2 (2 );
D11.fun1 (); // method in demo2 to override the method in demo1; otherwise, only the demo1 method is available.
System. Out. println ("------------");

Demo1 d111 = new demo3 (3 );
D111.fun1 (); // The method in demo1 must be rewritten in demo3; otherwise, only the demo1 method is available.
System. Out. println ("------------");

Demo2 D22 = new demo3 (2 );
D22.fun1 (1 );
D22.fun1 ();
D22.fun2 (2 );
D22.fun2 ();
D22.finalfun (); // In demo3, you must override the methods in the parent class. Otherwise, only all parent class methods are available. Other methods of your own cannot be called.
System. Out. println ("------------");
}/* Output:
Fun1 int I constend: 1
Fun1
~~~~~~~~~~~~~~
Fun1 int I constend: 2
Fun2 int I constend: 2
Fun1
Fun2
~~~~~~~~~~~~~~
Fun1 int I constend: 3
Fun2 int I constend: 3
Fun3 int I constend: 3
3 rewrite fun1
3 rewrite fun2
Fun3
~~~~~~~~~~~~~~
Fun1 int I constend: 2
Fun2 int I constend: 2
Fun1
------------
Fun1 int I constend: 3
Fun2 int I constend: 3
Fun3 int I constend: 3
3 rewrite fun1
------------
Fun1 int I constend: 2
Fun2 int I constend: 2
Fun3 int I constend: 2
Fun1 return int
3 rewrite fun1
Fun2 return int
3 rewrite fun2
Fun1 finalfun
------------

*///:~
}

.

#

#

#

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.