Extends and implements in Java

Source: Internet
Author: User

Beginners Java language, the code of extends and implements let me feel very confused, now finally understand the difference between them and usage.

[C-sharp]View PlainCopy
    1. Define a runner interface
    2. Public Inerface Runner
    3. {
    4. int ID = 1;
    5. void Run ();
    6. }

[Java]View PlainCopy
    1. Defines a interface Animal that inherits from the parent class runner
    2. Interface Animal extends Runner
    3. {
    4. void Breathe ();
    5. }

[C-sharp]View PlainCopy
  1. Defines the fish class, which implements the method of animal interface run () and breather ()
  2. Class Fish implements Animal
  3. {
  4. public Void Run () //Implements the animal method run ()
  5. {
  6. System.  out.println ("Fish is swimming");
  7. }
  8. Public void Breather ()
  9. {
  10. System.     out.println ("Fish is bubbing");
  11. }
  12. }
  13. An abstract class landanimal is defined, which implements the method of interface animal.
  14. Abstract Landanimal implements Animal
  15. {
  16. public void breather ()
  17. {
  18. System.  out.println ("Landanimal is Breathing");
  19. }
  20. }
  21. Defines a class student, which inherits the class person and implements the method run () of the runner interface.
  22. Class Student extends person implements Runner
  23. {
  24. ......
  25. public Void Run ()
  26. {
  27. System.  out.println ("The student is running");
  28. }
  29. ......
  30. }
  31. Defines an interface flyer
  32. Interface Flyer
  33. {
  34. void Fly ();
  35. }
  36. Defines a class bird that implements the methods defined by both the runner and flyer interfaces.
  37. Class Bird implements Runner, Flyer
  38. {
  39. public Void Run () //runner the method defined by the interface.
  40. {
  41. System.  out.println ("The bird is Running");
  42. }
  43. public Void Fly () //flyer the method defined by the interface.
  44. {
  45. System.  out.println ("The Bird is Flying");
  46. }
  47. }
  48. Testfish class
  49. Class Testfish
  50. {
  51. public static void Main (String args[])
  52. {
  53. Fish f = new fish ();
  54. int j = 0;
  55. j = runner.id;
  56. j = f.id;
  57. }
  58. }

The point of attention of the interface implementation:

A) implementing an interface is all of the methods to implement the interface (except abstract classes).
b) The methods in the interface are abstract.
c) Multiple unrelated classes can implement the same interface, and a class can implement multiple unrelated interfaces.

The difference between extends and implements:

Extends is inherited from the parent class, as long as the class is not declared final or the class is defined as abstract can inherit, Java does not support multiple inheritance, but can be implemented with interfaces, so that the use of implements, inheritance can inherit only one class, But implements can implement multiple interfaces, separated by commas on the line.

Like what:

Class A extends B implements C,d,e {} (class subclass name extends parent class name Implenments interface name)

The parent class differs from the subclass inheritance relationship:

A = new B (); Result A is an instance of Class A that can only access the method in a, and a = new A (); What's the difference?

***********************************************************************************************

Class B extends A
After inheritance, it is common to define members or methods that are not in the parent class.
A = new B ();
This is possible, upload.
A is an instance of a parent class object and therefore cannot access a new member or method defined by the child class.

***********************************************************************************************

If it is defined as:
Class A

{
int i;
void F () {}
}
Class B extends A

{
Int J;
void F () {}//Override
void G () {}
}
And then:
b b = new B ();
B is an instance of a subclass object that can access not only its own properties and methods, but also the properties and methods of the parent class. such as B.i,b.j,b.f (), B.G () are legal. At this point B.f () is the F () in the Access B


A = new B ();
A Although the constructor of B is used, it is upcast to become an instance of the parent object and cannot access the properties and methods of the child class. A.I,A.F () is legal, while A.J,A.G () is illegal. Access A.F () is at this point F () in Access B

***********************************************************************************************

A = new B (); There are actually three processes in this statement:
(1) A;
Declares a as a parent class object, but only a reference, unallocated space
(2) b temp = new B ();
An instance of Class B object is established by the constructor of Class B, that is, initialization
(3) A = (a) temp;
The Subclass object Temp is converted to the non-parent class object and assigned to a, which is the upload (upcast), which is safe.
After the above 3 processes, A has become an example of a class.
Subclasses tend to have more properties and methods than the parent class, uploads are only discarded, are safe, and the downcast sometimes increases and is usually unsafe.

***********************************************************************************************

A.F () corresponds to the method F () of Class B.
After invoking the constructor to establish an instance, the entry for the corresponding method has been determined.
In this case, A has been uploaded as Class A, but the overridden method F () is still the method F () of B. In other words, each object knows which method it should call.
A A1 = new B ();
A A2 = new C ();
A1,a2 both are Class A objects, but their respective f () are different. This is the manifestation of polymorphism.

***********************************************************************************************

Extends and implements in Java

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.