Java interface and abstract class small notes@author IxenosInterface
1. The interface does not have a constructor because the interface is not instantiated2. Anonymous objects if the constructor that uses the interface also simply represents a covariant anonymous object that implements the interface3. The member variables in the interface are the public sta
;publicstaticfinal that belongs to graphic class doublepi=3.14;publiccircle (Stringname,doubler) {super (name); THIS.R =r;} The area of Publicvoidgetarea () {System.out.println (name+) is: "+pi*r*r);} The perimeter of Publicvoidgetlength () {System.out.println (name+) is: ' +2*pi*r ';}} Rectangle belongs to the one classrectextendsmyshape{intwidth;intheight;public in the graph Rect (StrIngname,intwidth,intheight) {super (name);this.width=width;this.
One.There is also a part of inheritance called abstract class.Why abstract classes are present, for instance., Demoa and demob two classes have the same method, and if there is no inheritance relationship between them, they can be extracted upward. After extraction, subclasses inherit the parent class. and the contents of the subclass can be omitted to write, the
In object-oriented concepts, all objects are depicted by classes, but conversely, not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class.Abstract classes In addition to the object cannot be instantiated, other functions of the
barking, but how to make sure, this method teaches abstraction.When a class describes a thing, there is not enough information to describe the thing, and this class is abstract.Abstract class Canine {abstract void sing ();//four legs, two eyes, running, jumping ...} Class D
Not much to say, the first paragraph of code. Public classabstract{ Public Static voidMain (string[] args) {Cl2 c=NewCl2 (); C.method (); }}Abstract classabc{ Public Abstract voidmethod (); }classCl2extendsabc{ Public voidmethod () {System.out.println ("Concrete Method"); }}There can be no abstract method in an abstract
classcircleextendsmyshape{doubler; that belongs to graphic class publicstaticfinaldoublePI=3.14; Publiccircle (stringname,doubler) {super (name); this.r=r;}publicvoid The area of Getarea () {system.out.println (name+) is: "+PI*r*r); }publicvoidgetlength () { system.out.println (name+ "perimeter is:" +2*pi*r); }}//rectangle belongs to the in the drawing A classrectextendsmyshape{intwidth;int height;publicrect (stringname,intwidth,intheight) { super
Some important points to be aware of using abstract classes:
A class that contains one or more abstract methods must be declared as an abstract class.Declaring a class as an abstract class
1. Definition of abstract class(1) Abstract classes provide only partial implementations of a type. cannot be instantiated.(2) The relationship between abstract class and subclass----is an application of template method pattern2. Use of
as a parameter, and the value of the parameter is the value of the variable name.3. Variable name (variable name corresponding value) {Static code block}//except 2, the enumeration class also defines an abstract method, and the code block after the variable name corresponds to an anonymous inner class that implements the abs
at the definition of Myinvoke:Inner class Myinvoke:invocationhandler { Override Fun Invoke (Proxy:any, Method:method?, Args:array if (method?. Name.equals ("Dofail")) { LOG.I (TAG, "Dofail") }else if (method?). Name.equals ("DOSUCC")) { LOG.I (TAG, "DOSUCC") } }Last run, look at the result:I/testlib:mytest Start executingI/mainactivity:dofailI/mainactivity:dosucc2. Instance
Abstract classes are often used to characterize the abstract concepts we derive in the analysis and design of the problem domain, as an abstraction of a series of concrete concepts that look different, but are essentially the same, and we cannot instantiate them (not get a concrete one) so they are called abstractions. For example: We want to describe "fruit", it is an abstraction, it has quality, volume an
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.