| |

VerySource

 Forgot password?
 Register
Search
View: 2311|Reply: 11

Hello everyone, I want to ask a confusion about the interface

[Copy link]

2

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-17 07:00:01
| Show all posts |Read mode
Hello everyone, I want to ask a confusion about the interface
For example, there is an interface:
interface IPeople{
void eat();
void sleep();
}
Another class implements the interface IPeople
class Boy implements IPeople
{
public void eat(){System.out.println("Eat........");}
public void sleep(){System.out.println("Sleep........");}
}

There is another main class
class Test{
public static void main(String[] args){
Boy b=new Boy();
b.eat();
b.sleep();
}
}
This program completes the eat and sleep methods in the Boy class, so what is the point of implementing the interface IPeople?
I do not implement the interface, I can also new a Boy class, and then call the eat and sleep methods
Reply

Use magic Report

0

Threads

60

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-12-17 13:45:01
| Show all posts
So what is the point of implementing the interface IPeople

Your interface is implemented by only one class, of course it is of little significance.
If you have a class that also implements this interface,

class Boy1 implements IPeople
{
public void eat(){System.out.println("Eat........");}
public void sleep(){System.out.println("Sleep........");}
}


Main program call

class Test{
public static void main(String[] args){
IPeople b=new Boy();
b.eat();
b.sleep();
}
}

If you want to modify it later,
You can just change it
       IPeople b=new Boy1();

Do you think this is very convenient to maintain, and of course it can be extended
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-17 18:45:01
| Show all posts
When your implementation changes, it does not affect your other code.
Can reduce the degree of coupling between classes.
Reply

Use magic Report

2

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-12-17 20:30:01
| Show all posts
Brother still don't understand
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-18 02:00:02
| Show all posts
IPeople boy=new Boy();
Reply

Use magic Report

1

Threads

60

Posts

37.00

Credits

Newbie

Rank: 1

Credits
37.00

 China

Post time: 2020-12-18 02:45:01
| Show all posts
Class inheritance is tightly coupled, interface inheritance is loosely coupled
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-18 21:30:02
| Show all posts
Can be injected
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-18 21:45:01
| Show all posts
Layered development
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-12-19 19:30:01
| Show all posts
If you only look at the function of this simple example, it may not be necessary, but it is very necessary to consider the development idea! !
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-12-19 20:00:01
| Show all posts
Deep understanding of abstract class and interface
Abstract class and interface are two mechanisms for supporting abstract class definitions in the Java language. It is precisely because of the existence of these two mechanisms that Java has a strong object-oriented capability. Abstract class and interface have great similarities in their support for abstract class definitions, and they can even replace each other. Therefore, many developers seem to be more casual in choosing abstract class and interface when defining abstract classes. In fact, there is still a big difference between the two. Their choice even reflects the understanding of the nature of the problem domain and whether the understanding of the design intent is correct and reasonable. This article will analyze the differences between them, trying to provide developers with a basis for choosing between the two.


Understand abstract classes

Abstract class and interface are both used for abstract classes in the Java language (the abstract class in this article is not translated from abstract class, it represents an abstract body, and abstract class is used to define abstract classes in the Java language One way, readers should pay attention to the distinction), then what is an abstract class, and what benefits can using abstract classes bring us?

In object-oriented concepts, we know that all objects are described by classes, but the reverse is not true. Not all classes are used to describe objects. If a class does not contain enough information to describe a specific object, such a class is an abstract class. Abstract classes are often used to characterize the abstract concepts we get in the analysis and design of the problem domain. It is an abstraction of a series of concrete concepts that look different but are essentially the same. For example: If we develop a graphics editing software, we will find that there are specific concepts such as circles and triangles in the problem domain. They are different, but they all belong to the concept of shape. The concept of shape is not in the problem domain. Existing, it is an abstract concept. It is precisely because abstract concepts have no corresponding specific concepts in the problem domain, so abstract classes used to represent abstract concepts cannot be instantiated.

In the object-oriented field, abstract classes are mainly used for type hiding. We can construct an abstract description of a fixed set of behaviors, but this set of behaviors can have any number of possible concrete implementations. This abstract description is an abstract class, and this set of any possible concrete realizations is represented as all possible derived classes. The module can manipulate an abstract body. Because the module depends on a fixed abstract body, it can not be modified; at the same time, by deriving from this abstract body, the behavior and functions of this module can also be extended. Readers familiar with OCP must know that in order to be able to realize one of the most core principles of object-oriented design, OCP (Open-Closed Principle), abstract classes are the key.


Look at abstract class and interface from the grammar definition level

At the grammatical level, the Java language gives different definitions for abstract class and interface. The following takes the definition of an abstract class named Demo as an example to illustrate this difference.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list