(); - } + Abstract classBextendsa{ A Public Abstract voidmethod2 (); at } - classCextendsb{ - Public voidmethod1 () { - //C method1 - } - Public voidmethod2 () { in //C method2 - } to}For Class C, there will be no chance of inheriting other parent classes.To sum up, interfaces and abstr
method to the interface can be buffered in the abstract class that implements the interface, providing a default implementation, so that you do not have to enforce all subclasses ( classes that indirectly implement an interface by inheriting an
{ publicstaticvoidmain (String[]args) { animala1=newcat (); animala2=newdog (); a1.cry (); a2.cry (); }} -------------------------------------------------------------------- Output: cat bark: Meow ... Dog Bark ...
It is useful to create abstract classes and abstract methods because they can make the abstraction of the class clear and tell the user and
) {//There are 6 passengers who want to travel, no emphasis on the way of travel, random distribution of vehiclesRandom random =NewRandom (); Passenger[] Passengers=NewPASSENGER[6];//Declaration of 6 passengers for(inti=0;i) {Passengers[i]=NewPassenger ("passenger[" +i+ "]"); } icarrypassenger[] ICP=NewICARRYPASSENGER[3];//declare 3 modes of transportationIcp[0] =Newairplane (); icp[1] =NewCar (); icp[2] =NewTrain (); for(inti=0;i) {Passengers[i].travelby (Icp[random.nextint (3)]); }
resides must be an abstract class. Second, what is the interface1, the background (why have interface): Java does not have multiple inheritance concept, but the real life of the object exists in this phenomenon, so there is an interface.For example: "Cat" belongs to "animal", also belongs to "cat" but in
can realize this interface, not flying will not implement this interface.2) The design level is different, abstract class as the parent class of many subclasses, it is a kind of template design. And the interface is a kind of beh
();}A class to implement a method in which an interface must implementInheritance: Multiple inheritance is not supported in Java, a class can inherit only oneInterfaces: Interfaces can implement multiple// Interface 1 Public Interface
Just learning Java, always feel that the interface and abstract class is very similar, but it is not specific what the difference. Today calm down, turn over the book, check the information, do a summary. Let's start with two examples to see what the similarities and differences between
. Details p163Nineth Chapter Interface1. Abstract classClasses that contain abstract methods are called abstract classes. Or it may not contain abstract methods (as if it makes no sense), as long as the class is decorated with an abstract.Abstract classes are useful tools fo
There are two mechanisms for Java to provide multiple implementations of an abstraction-Interface and abstract class.Interface and abstract class,In addition to the obvious differences (i.e., the ability to provide a basic implementation),The important difference is that the
I may be asked frequently during interviews. What is the function of interfaces? Compared with the class, why do you need to use the class to implement interfaces and so on. If you don't think about it, sometimes it gets stuck. This articleArticleThis section briefly introduces the functions of interfaces.An interface is a product used to mark a
implements Animal{ public void breathe(){ System.out.println("I‘m breathing"); } void eat(){ System.out.println("I‘m eating"); }}public class Test{ public static void main(String[] args){ Dog dog = new Dog(); dog.breathe(); dog.eat(); }}```No: Methods in an interface are modified by public by default, so when a subclass implements an
1. There is at least one method that is modified by the abstract, while the class that modifies the class name is an abstraction class, the abstract method must be covered by the quilt class, the
() { System.out.println ("Calling A.foo ()");} } public class Clazz implements A {} Even if the Clazz class does not implement the Foo () method. The default implementation of the Foo () method is provided in interface A. This is the default method .(2) The static method cannot be defined in the interface, a
Usage:In abstract class mode, a class can have its own data member, or it can have a non-abstract member method, and in the implementation of the interface method, the class can only have static data members that cannot be modifie
Interface
1. Because Java does not support multiple inheritance, an interface can inherit only one parent class, but multiple interfaces can be implemented. The interface itself can inherit multiple interfaces.
2. The member variables in the
Click to enter _ many other _java thousand ask1. What is the difference between an interface and an abstract class?In the Java language. Abstract class and interface
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.