Abstract class and Interface learning Summary

Source: Internet
Author: User

An abstract class can be called a template, defining the most basic characteristics of all the subclasses of that base class, as defined by what is called an animal, what is a plant, so some properties and methods are intrinsic, and can be defined in an abstract class, as inherited by previous studies, passing on something very useful. But there are some things that are not certain, such as what the animal eats and so on, is still not certain, but this is its inherent and method, at this time we can use the keyword abstract to define it as an abstraction method, waiting for their subclasses to implement, and its subclasses must implement these abstract methods, Otherwise cannot be created instance, still belong to abstract class. Abstract classes apply to very strong parent-child relationships.

    • Abstract classes can have no abstract methods, but classes with abstract methods must belong to abstract classes;
    • Once a construction method is defined in an abstract class, the real class must show the construction method that overrides it, even if it is super ();
    • Abstract methods have no method body, so they cannot appear {}; The notation needs to be as follows: Public abstract void Eat ();
    • Abstract classes can have variables and non-abstract methods;

Interfaces are different from abstract classes, interfaces do not have strong parent-child relationships, and more simply describe certain classes that inherit the interface and perform some kind of action, such as plants and animals that inherit a feeding interface, which indicates that both plants and animals have the same feeding action;

    • Interfaces are constants and abstract methods, the default modifier is public static final and public abstract , can be added without;
    • Interfaces can inherit other interfaces, and can be multiple;
    • Only a single class can be inherited, but multiple interfaces may be inherited;

About java.lang.Cloneable This interface, which does not implement any method, just as an identity interface, the class inheriting this interface can be copied through. Clone (), the two points to a different object, but note that When an object is referenced to one of the properties within the object, the clone is simply a copy of its reference, and the content is not copied, only the clone is called a shallow clone, and the implementation of the deep clone can be rewritten by itself. Clone () method;

The following code demonstrates deep cloning and shallow cloning: when deep cloning is implemented, all objects in the copied content are implemented with the Serializable interface, which reads the content and saves it to another object through the stream.

 Public class Implements serializable{     publicint a = 1;       Public C (int  ab) {         this. A = ab;     }} 
 Public classEntityImplementsserializable,cloneable{ Publicc C =NewC (1);  PublicObject Deepclone ()throwsioexception,classnotfoundexception {bytearrayoutputstream BOS=NewBytearrayoutputstream (); ObjectOutputStream Oos=NewObjectOutputStream (BOS); Oos.writeobject ( This); //read it back from the stream.Bytearrayinputstream bis =NewBytearrayinputstream (Bos.tobytearray ()); ObjectInputStream Ois=NewObjectInputStream (bis); returnOis.readobject (); }     PublicObject Clone () {object o=NULL; Try{o=Super. Clone (); } Catch(clonenotsupportedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returno; }}
1  Public classentitytext{2 3       Public Static voidMain (string[] args) {4Entity E =Newentity ();5          Try {6Entity e1 =(Entity) E.clone ();7Entity e2 =(Entity) E.deepclone ();8E.C.A = 2;9 System.out.println (E1.C.A);Ten System.out.println (E2.C.A); One}Catch(IOException e) { A             //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(ClassNotFoundException e) { the             //TODO auto-generated Catch block - e.printstacktrace (); -         } -           +      } -}

Abstract class and Interface learning Summary

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.