Abstract classes and Interfaces

Source: Internet
Author: User
Tags shallow copy

Abstract class Keywords: abstraction Note: cannot have static modifier interface keyword: interface is used to resolve problems that cannot be multiple inheritance Note: The interface modifier can only be the default or the public interface variable is automatically set to static public final            。 interface-defined methods are abstract and automatically set to public. Interface implementation: Implements a class can implement multiple interfaces comparison a class can implement multiple interfaces, but only one abstract class is inherited. Abstract classes can have non-abstract methods, and the methods defined in an interface must be all abstract methods. Methods defined in an abstract class whose modifiers can be public, protected, private, or default, but all methods in the interface must be public. Abstract classes can have constructors, interfaces cannot. Neither can be instantiated, but it is possible to store the subclass object or the object of the implementation class through them. It can be said that they can all achieve polymorphism. Cloning clone () requires implementation of the Cloneable interface
Package com.mypackage.interfaced;


public class Human implements cloneable{
String name;
String sex;
int age;

Human () {}

Human (String name,string sex,int age) {
THIS.name = name;
This.sex = sex;
This.age = age;
}


void work () {}
void Eat () {}

@Override
Protected Object Clone () throws Clonenotsupportedexception {
TODO auto-generated Method Stub
Human h = null;
try{
h = (Human) super.clone ();
}
catch (Clonenotsupportedexception e) {
E.printstacktrace ();
}
return h;
}
}

Human Zhangyu = new Human ("Zhangyu", "NV", 24);
Human lala = (Human) zhangyu.clone ();
Lala.name = "Lala";
System.out.println ("lala:" +lala.name);
System.out.println ("Zhangyu:" +zhangyu.name);

But this only completes the shallow copy, for the variable object variable, he can only get the address of the object, the original object and the cloned object still contains the Mutable objects still point to the same object, so changing one of the objects will still change the other. But although string appears as an object, but because of his immutability, string does not cause problems in cloning. The workaround is to also implement cloning for the object variable.

Abstract classes and Interfaces

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.