Differences between Object Instantiation using base classes and interfaces

Source: Internet
Author: User

The code is clear.

Write a base class fatherclass. Java

package com;public class FatherClass {public void speak(){System.out.println("Father method,speak() ");}public void sing(){System.out.println("Father method,sing() ");}public void jump(){System.out.println("Father method,jump() ");}public void run(){System.out.println("Father method,run() ");}}

Write an interface motherinterface. Java

package com;public interface  MotherInterface {public void cook();public void clean();public void love();}

Write a specific class sonclass. Java

Package com; public class sonclass extends fatherclass implements motherinterface {@ overridepublic void Cook () {// todo auto-generated method stubsystem. out. println ("Son method Cook ()") ;}@ overridepublic void clean () {// todo auto-generated method stubsystem. out. println ("Son method clean ()") ;}@ overridepublic void love () {// todo auto-generated method stubsystem. out. println ("Son method love ()");}////// /////////// Overload base class method ////////////////////// /// // public void jump () {system. out. println ("Son method, jump (), extends fatherclass");} public void run () {system. out. println ("Son method, run (), extends fatherclass ");} /// // write your own method //////////////// //// // public void mymehtodd () {system. out. println ("Son method, mymehtodd (), my own method");} public static void main (string [] ss) {// use a base class instance object, object only There are base class methods // (if the base class method is reloaded, the overload method is used) fatherclass fc = new sonclass (); // interface instance object, the object only has interface methods // These methods must implement motherinterface MI = new sonclass (); // use this class instance object, there are both base class methods (there are overload methods that take the heavy load ); // There are also interface methods (interface methods must be implemented), and their own unique methods of this class. Sonclass son = new sonclass (); system. out. println ("-------- base class instance object FC ------------"); FC. speak (); FC. sing (); FC. jump (); FC. run (); system. out. println ("-------- interface instance object mi ------------"); Mi. cook (); Mi. clean (); Mi. love (); system. out. println ("-------- the object son ------------"); son. speak (); son. sing (); son. jump (); son. run (); son. cook (); son. clean (); son. love (); son. mymehtodd ();}}

Console printing:

-------- Base class instance object FC ------------
Father method, speak ()
Father method, sing ()
Son method, jump (), extends fatherclass
Son method, run (), extends fatherclass
-------- Interface instance object mi ------------
Son method Cook ()
Son method clean ()
Son method love ()
-------- Son ------------ object of this class instance ------------
Father method, speak ()
Father method, sing ()
Son method, jump (), extends fatherclass
Son method, run (), extends fatherclass
Son method Cook ()
Son method clean ()
Son method love ()
Son method, mymehtodd (), my own Method

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.