Java class inheritance, polymorphism, abstract classes and interfaces

Source: Internet
Author: User

Knowledge Point carding:
1, how to define your own class.
Mystarframe,mystarpanel
Classes are defined in:
(1) attribute (data), variable.
(2) method (function), behavior.
(3) Construction method (feature, function, when called, how to write the method of construction).

2, inherit from the already existing class. (API, written by yourself).
Write Inheritance:
public class A extends b{
Overrides of the/** method, overriding
* A method in the parent class that has the same name as the same argument.
*/
public void paint () {
}

A new method of/** extension
*/
public void Panit (Graphics g) {
}
}

A = new A ();

Code reuse.
This: current object
Super: Parent Class object

Class a{
private int ABC;

public void M1 () {
this.abc=10;
}
}
...
A = new A ();
A.M1 ();
A B = new A ();
B.M1 ();
...

===========================================
A->b->c->d
How to use:
1, thread, inner class, awt,swing,...

Object-oriented:
1, how to define classes: properties and methods
2, what is the construction method
3, how to generate the object, the syntax and the steps to generate the object.
4, how to inherit an already existing class.
5, the benefits of inheritance.
6, method of coverage.
===========================
The inheritance relationship is
A "is a" B
A extends B
Association (aggregation):
A "has a" B
Access Control Modifiers:
Members in the class: Properties and Methods
Class a{
public int a;//is accessible from anywhere
protected int b;//Package visible, child class visible
int C; Visible in Package
private int d;//can only be accessed within this class
Provides a syntactic basis for object encapsulation.
===========================
The basis of the class:
Classes are encapsulated, and classes produce the encapsulation of data.
Student Performance Management:
1, Student score: int,double
2, account name: String
3, Student:
Private School Number: String,long
Private Name: String
Private sex: Char
Private Age: Int ( -2^31~+2^31-1)
Private class: String
Private Grade: String
Private exam Results: int
public static int abc;//static property
Uniform Rules to access,
Public method: You want external access to the
===========
Private method: You do not want external access, you just provide services for internal other methods.
Human: \
public void meal () {
1, find a place
2, order
3, serving
4, open Eat
5, digestion
6, excretion
}

private void Digest () {

}
......
=========================
Static
All members of the class (properties and methods) are divided into 2 types:
Static member = Class Member
Describes the characteristics of the class itself.
Access is accessed using the class name directly:
A.abc
A.M1 ();
There is only one copy of the entire program, which is initialized when the class is loaded.

Non-static member = Instance member = Object Member
Describing the characteristics of an object's individual
The object name is required to access:
A a=new a ();
A.def;
A.M2 ();
How many instances of an object have the instance properties that are created when the object is generated.
==================
Java ABC
A Java program run-time procedure:
1. Start the JVM.
2. Load the class to run (Abc.class)
3. Find the main function of the class and run the main function
==================================
About Inheritance of Java
1,java is a single inheritance, and each subclass has only one parent class, and the entire inheritance structure is a tree.
The root of the tree structure is object.
2, Benefits: The inheritance relationship is simple and clear.
3, Disadvantage: You can inherit functionality from only one parent class.
Car:move
Weapon:attack
Tank extends CAR implements weapon
================================
What is polymorphic?
One object in many forms.
An inheritance relationship is the basis of an object's polymorphism.
"is a" relationship
Bird b=new Bird ();
Animal a=new Bird ();
Object o=new Bird ();
Basic Rule (theorem):
1, if we look at a class object as a parent object, we can only access the properties and methods defined in the parent class. You cannot access the properties and methods of a subclass extension.
Animal a=new Bird ();
A.move ();
A.eat ();
A.singsong ();//Cannot access
Person
Javateacher Teachingjava ()

2, when the subclass overrides the method of the parent class, if we look at the class object as the parent class object, to call the method, we call the method after the subclass overwrite.
Animal a=new Bird ();
A.move ();//Output flying!

animal--Eat ()
Horse---eat () {"Eat Grass"}
Lion---eat () {"Eat meat"}

Animal a=new Bird ();
1, subjective view objectively
2, compile-time type run-time type
Compile-Time Type: What does the compiler think of a?
Runtime type: What does the JVM think of a?
A.singsong ();//
==============================
Abstraction and Interfaces
A specific class that describes a specific thing.
Animal: Abstract class, method not achievable (abstract)

=================================
Abstract classes and Interfaces
Real-time strategy game: StarCraft, Empire,...
Different classes, attacking.
Tanker
Machine gun
Fighter
.....
Public abstract class person{
... ....
public abstract void Attack ();
}

public class Tank extends person{
public void Attack () {
Firing shells
}
}

public class Flighter extends person{
public void Attack () {
Launching missiles
}
}

Person[] Army=new person[10];
Army[0]=new Tank ();
Army[1]=new Flighter ();
Army[3]= ....
... ...

for (int i=0;i<army.length;i++) {
Army[i].attack ();
}
==============================
Interfaces: Special abstract classes
1, all methods in an interface are abstract methods.
2, the interface does not have an instance property, all properties are static final
3, interface has no construction method
4, a class can implement multiple interfaces at the same time

Why should I use an interface?
1, to complement the single inheritance structure.
2, the interface enriches the polymorphism of the object.
3, interface-oriented programming (programming or architectural thinking)

The goal of software development:
A center, two basic points.
Everything is centered on user needs,
Ensure the software has good scalability and maintainability
Cohesion-poly, low-coupling
Cohesion: The ability of a software system to accomplish a task independently, without requiring a small amount of external involvement.
Car:
Manual---auto----driverless
5 3 1

Coupling degree: The complexity of the relationship between modules and modules, classes and classes.
A->b->c->d->e (safest)
The interface is stable, unchanging, abstract.

If there is an imperfect place please make more comments, please indicate the source when forwarding!

Java class inheritance, polymorphism, abstract classes and interfaces

Related Article

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.