Java Abstract classes and interfaces

Source: Internet
Author: User

Java Abstract classes and interfaces

1. abstract class/** 1. abstract METHODS must exist in abstract classes. abstract methods and abstract classes must be modified! 2. abstract classes cannot be instantiated. 3. If multiple abstract methods exist in an abstract class and its subclass only inherits one abstract method, the subclass is an abstract class and therefore cannot be reinforced. 4. An abstract class can only create a subclass object if the quilt class overwrites all its abstract methods! 5. the abstract class may contain non-Abstract METHODS * // This subclass does not overwrite the abstract method of the parent class func1. Therefore, the subclass is an abstract class and cannot be instantiated. Compilation failed abstract class Person {abstract void func (); abstract void func1 (); void func2 () {System. out. println ("non-abstract method 2! ") ;};} Class Student extends Person {public void func () {System. out. println (" non-abstract method! ") ;}} Class AbstractDemo {public static void main (String [] args) {Student st = new Student (); // The subclass is an abstract class and cannot be instantiated. func () ;}/// this parent class does not contain abstract methods, the class name is not abstract modified, the compilation fails class Person {abstract void func (); // abstract void func1 (); void func2 () {System. out. println ("non-abstract method 2! ") ;};} Class Student extends Person {public void func () {System. out. println (" non-abstract method! ") ;}} Class AbstractDemo {public static void main (String [] args) {Student st = new Student (); st. func () ;}// compiled successfully abstract class Person {abstract void func (); // abstract void func1 (); void func2 () {System. out. println ("non-abstract method 2! ") ;};} Class Student extends Person {public void func () {System. out. println (" non-abstract method! ") ;}} Class AbstractDemo {public static void main (String [] args) {Student st = new Student (); // The subclass is an abstract class and cannot be instantiated. func () ;}} 2. interface/** 1. all methods in the interface are abstract methods, so they can be considered as abstract classes. 2. the interface member has a fixed modifier constant: public static final method: public abstract. All methods in the interface have the public permission. */Interface Inter {public static final int NUM = 3; public abstract void show ();} class SubInter implements Inter {public void show () {System. out. println ("implementation interface! ");} Class InterfaceDemo {public static void main (String args []) {SubInter sb = new SubInter (); sb. show ();}

 

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.