Java Basics 6--Abstract classes, abstract methods, interfaces, construction methods, class methods and other easily confusing knowledge points

Source: Internet
Author: User

I. Abstract classes and abstract methods (B inheritance abstract Class A)

Abstract classes: Abstract methods must be abstract

Abstract method: The method name has an abstract adornment, and the method has no method body, that is {}, does not need to implement any function, just declares

1. There are two methods in an abstract class:

(1) The ordinary method in the abstract class, can not implement

(2) Abstract method, the abstract class is inherited, the abstract method must be overridden, unless it is inherited by the abstract class, do not have to be

Abstract classes must be inherited, and abstract classes cannot be instantiated, i.e. abstract class A, which cannot define a a=new a ();

 PackageInterface;/*** Created by Wyy on 2017/2/25.*/ Public classAbstracttest { Public Static voidMain (string[] args) {person S=NewStudents ();//object referencing the studentsPerson T =NewTeacher ();//object referencing the teachers.f ();   T.F (); //I am a student, I am a teacher    }}    Abstract classPerson { Public Abstract voidf ();//declares an abstract class    }    classStudentsextendsperson {//class students inheriting abstract classes         Public voidf () {System.out.println ("I am a student"); }    }    classTeacherextendsperson {//class teacher inheriting abstract classes         Public voidf () {System.out.println ("I'm a teacher."); }    }

Two. Interface (B implements interface A)

To implement an interface class, you must implement all the methods defined within the interface.

First define an interface class like

 Package Interface; /**  */public    interface  like{      publicvoid a ();        Public void b ();    }

Interface Class Hate

 Package Interface; /**  */Publicinterface  Hate    {publicvoid  C () ;      Public void d ();}

To implement both interfaces, you must implement all the methods within the interface:

 Public classAImplementslike,hate {@Override Public voidA () {System.out.println ("This is a"); } @Override Public voidB () {System.out.println ("This is B"); } @Override Public voidC () {System.out.println ("This is C"); } @Override Public voidd () {System.out.println ("This is D"); }}

Three. Construction methods and class methods

(1) Construction method, inheritance (super,this keyword) Construction method: The method name and class name are the same, generally used to create objects when the assignment, commonly used in This.name=name
    • The method name is the same as the class name;
    • Do not return types (for example, return, void, etc.);
    • cannot be modified by static, final, native, abstract and synchronized, and cannot be inherited by a quilt class.
The construction method of the parent class cannot be called by the class, and the constructor of the parent class can be called through the Super statement. The construction method of calling the parent class with super must adhere to the following rules: 1. The constructor method of the subclass cannot be called directly from the parent class's method name, using the Super statement; 2. The addition of a super statement in the constructor of a subclass must be used as the first statement of the constructor (the same this statement) (2) class method: A static-modified method that can be called directly with the class name without creating an instance-loaded reference Four. Method overloading and method rewriting(1) Overloading: Overload is mainly for the method of the same name in the class, but its method parameter type, parameter number, parameter order, return data type is different, such as: Void Method (), void method (int i), void method (int a,int b) , String method (); The most common overload is System.out.println () is actually a class with more than one name, but the parameters and return values may not be the same. (2) Rewrite: Subclasses override the parent class method, or implement an excuse method, its method name, parameter number, parameter type, return data type must be exactly the same, another: The overridden method throws an exception that cannot be more extensive (at least the same or its exception subclass) than the parent class. In fact, the word is: method Overload: In the same class, the name of the method is the same, but the number of parameters, the type of the parameter or the return value type is different! Method overrides: It refers to the relationship of the child class and the parent class, and the subclass overrides the method of the parent class, but the method name, parameter type, and number of arguments must be the same! Overrides of methods (overriding) and overloads (overloading) are different manifestations of Java polymorphism, which is a representation of polymorphism between parent class and subclass, and overloading is a representation of polymorphism in a class. Five. polymorphismThree necessary conditions for polymorphic existence
    • Inherited
    • Overrides (subclass inherits parent class, but has its own different methods of handling)
    • Parent class reference to child class object
A extends BB b=new a (); Parent class reference to child class object when you call a method using polymorphic mode, first check whether the method is in the parent class, if not, compile the error, and if so, then call the method with the same name as the subclass. That is: If the parent class calls a method of a subclass, if there is no such method in the parent class, the child class refers to the method of the parent class with super, referring to its own method with this, overriding method refers to the implementation within the method can be different from the parent class to complement the later ~

Java Basics 6--Abstract classes, abstract methods, interfaces, construction methods, class methods, and other easily confusing points of knowledge

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.