The examples in this article describe the Java interface and abstract class usage. Share to everyone for your reference, specific as follows:
Interface
1 because Java does not support multiple inheritance, there is an
can contain only constants and abstract methods-data defaults to constants (public static final).Method defaults to abstract (public abstract)4) interface cannot be instantiated5) The interface needs to be implemented, the subclass implements the
For object-oriented programming, abstraction is one of its three main features (abstraction, inheritance, polymorphism). In Java, there are two ways to embody the abstraction of OOP: interfaces and abstract classes.The two are both similar and different. You will be silly when you are beginners the difference between the interface and the
Abstract class and interface support the definition of abstract classes in Java. The existence of these two mechanisms gives Java powerful object-oriented capabilities. Abstract
necessarily include abstract methods. In other words, abstract classes can have no abstract abstraction method.3. Once the class contains an abstract abstraction, the class must be declared a
"name =" + super. getname () + ", age =" + super. getage ();
}
}
Public class demo05
{
Public static void main (string ARGs [])
{
Student s = new student ("James", 30 );
System. Out. println (S. getinfo ());
}
}
3. interface:
3.1 interfaces are the combination of Abstract methods and Constants
Interface definition met
(Request,response);}
catch (excetpion e) {
????
Log exception information
}
? ? ? ? ? ?}
? ? ? ? ? ?}
protected abstract void Doservice (HttpServletRequest request, httpservletresponse response) throws Ioexcetion, servletexception;//Note that access rights are defined as protected, rigorous, because it is intended for subclasse
).
Multiple inheritance of interface
In the inheritance of Java classes, a derived class can have only one base class. In other words, a class cannot inherit more than one class at a time. In
The protagonist: abstract class, interface. For Java beginners such as Pippi, interfaces and abstract classes can be difficult to figure out without spending a lot of effort and time, and I also feel that I am unfamiliar with these two concepts in the recent week of project
object type, but if a parent class is too many subclasses, this kind of judgment is very cumbersome * How to design the parent class? * 1. The parent class is typically designed as an abstract class or interface, where the
classes can have constructors
Interfaces cannot have constructors
Differences from normal Java classes
Except that you can't instantiate an abstract class, it's no different than a normal Java class.
Interfaces are completely different types
OriginalWhat's the difference between an interface and a abstract class in Java?It's best to start answering this question with a brief definition of an abstract classes and interfaces and then explore the Differences between the other.A
1. The difference between abstract classes and interfacesThe so-called abstract class is used to characterize our analysis in the problem domain, the abstract concept of design, is a series of seemingly different, but essentially the same specific concept of abstraction; the so-called
partially rewritten.5. A class that contains one or more abstract methods must be defined as an abstract class.6.abstract and final cannot modify the same class at the same time.7.abstract
some methods, while Java interfaces cannot (that is, only methods can be defined in the interface, but not the implementation of methods, but in the abstract Class can have both the specific implementation of the method, and there is no concrete implementation of the abstract
One, polymorphic1. Polymorphism exists in inheritance and interfaces.2. The indeterminate behavior is placed in the parent class.3. Subclasses must override the indeterminate behavior in the parent class.Second, abstract class1. Keyword abstract.Example: Public abstract class shap{public
Java polymorphism, interface, abstract class awareness, java Polymorphism
1,
Polymorphism: the reference of the parent class points to the subclass object, which has inheritance and rewriting.
Polymorphism: cat is an Animal
Rule:
the ID is static by default, it is called directly by runner.J = f. ID;J = fish. ID; // because the fish class inherits the parent class, you can reference it like this.F. ID = 2; // This error is returned because ID is a constant and cannot be assigned a value.}}Abstract class landanimal implements animal // because
Java abstract classes and interfaces can implement the separation of functionality and implementation, both of which provide a good support for polymorphism, then we should use the abstract class or interface. In a previous article on the
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.