Object-oriented _ multi-state _ abstract Class _ Interface

Source: Internet
Author: User

09.01_ Object-oriented (an overview of polymorphism and its code embodiment)

A: Polymorphism (polymorphic) Overview
The various forms of the existence of things
B: Multi-state premise
A: To have an inheritance relationship.
B: There are methods to rewrite.
C: To have a parent class reference to the child class object.
C: Case Demo
Code manifests polymorphism

 Public  class demo1_polymorphic {    /** * Member variables: At compile time See Left (parent Class), runtime look Left (parent Class) * Member method: At compile time look to the left (parent class), runtime see right (subclass) * @param args */     Public Static void Main(string[] args) {//TODO auto-generated method stubFather f =NewSon ();//Parent reference to sub-class object, Superman promoted to humanSystem.out.println (F.num);//10F.print ();//son}}class father{intnum =Ten; Public void Print() {System.out.println ("Father"); }}class Son extends father{intnum = -; Public void Print() {System.out.println ("Son"); }}
09.02_ Object-oriented (member variable of member access characteristics in polymorphism)

Member variables
compile to see left (parent Class), run look Left (parent class).

09.03_ Object-Oriented (member method of member access characteristics in polymorphism)

Member Methods
compile to see left (parent Class), run to see right (subclass).

09.04_ Object-oriented (static member method of member access characteristics in polymorphism)

Static methods
compile to see left (parent Class), run look Left (parent class).
( static and class-dependent , not rewritten, so access is still on the left)
So, only non-static member methods, compile look left, run look right

09.05_ Object-Oriented (Superman's story)
A: Case Study
Use this case to help students understand polymorphic phenomena

09.06_ Object-oriented (transition up and down in polymorphic)

A: Case Demo
Learn more about transition up and down in multi-state
Person P = new Superman ();
Superman sm = (Superman) p; downward transition

09.07_ Object-oriented (polymorphism benefits and drawbacks)

A: The benefits of polymorphism
A: Improved maintenance of code (inheritance guarantee)
B: Improved code extensibility (guaranteed by polymorphism)

B: Case Demo
The benefits of polymorphism
Can be used as a formal parameter, can receive arbitrary sub-class objects

C: The drawbacks of polymorphism
You cannot use the unique properties and behaviors of subclasses.

D: Case Demonstration Method (Animal a) method (Cat C)

Package day09_polymorphic; Public  class demo2_usepolymorohic {     Public Static voidMain (string[] args) {//TODO auto-generated method stubMethodNewCat ()); MethodNewDog ());//animal a = new Cat (); In development, it is seldom to directly use the parent class to refer to the subclass object, but to create the subclass object directly, so that you can use the properties and methods peculiar to the subclass} Public Static voidMethod1 (Animal a) {a.eat (); }//This is where polymorphic usage is!!!     //Can be used as formal parameters, accept any type of sub-class object, strong extensibility    //When you come back to a pig, add a class directly, without having to change other classes and methods.     //But there are drawbacks: you cannot use unique methods of objects. You can only use a downward transformation and then call the corresponding method    //instanceof Determines whether the preceding type is not the following type     Public Static voidMethod (Animal a) {if(A instanceof Cat)            {Cat c = (cat) A;            C.eat ();        C.catchmouse (); }Else if(A instanceof Dog)            {Dog d = (dog) A;            D.eat ();        D.lookhouse (); }Else{a.eat (); }    }} class Animal{     Public voidEat () {System.out.println ("Food for the Animals"); }} class Cat extends Animal{     Public voidEat () {System.out.println ("Cat eats fish"); } Public voidCatchmouse () {System.out.println ("Catch the mouse."); }} class Dog extends Animal{     Public voidEat () {System.out.println ("Dog eats meat."); } Public voidLookhouse () {System.out.println ("Housekeeping"); }}
09.08_ Object-oriented (multi-state problem analysis)

A: see if there is a problem with the procedure below, if not, say the result
Class Fu {
public void Show () {
System.out.println ("Fu Show");
}
}

Class Zi extends Fu {
public void Show () {
System.out.println ("Zi Show");
}
public void Method () {
System.out.println ("Zi method");
}
}

Class Test1demo {
public static void Main (string[] args) {
Fu f = new Zi ();
F.method ();//Error!
F.show ();
}
}
Copy Code

B: See if there is a problem with the procedure below, if not, say the result

 class A {     Public voidShow () {Show2 (); } Public voidShow2 () {System.out.println ("I"); }} class B extends A {//No Show () to inherit from parent class//equivalent to having/*public Void Show () {Show2 (); }*/     Public voidShow2 () {System.out.println ("Love"); }} class C extends B {     Public voidShow () {Super. Show (); } Public voidShow2 () {System.out.println ("You"); }} Public  class test2duotai {     Public Static voidMain (string[] args) {A A =NewB ();        A.show (); b b =NewC ();    B.show (); }}//Result: Love you
09.09_ Object-oriented (an overview of abstract classes and their characteristics)

A: Abstract class overview
Abstraction is what you can't read.
B: Abstract class features
A: Abstract classes and abstract methods must be decorated with the abstract keyword
Abstract class class name {}
public abstract void Eat ();

B: Abstract classes do not necessarily have abstract methods, classes with abstract methods must be abstract classes or interfaces

C: Abstract classes cannot be instantiated . So, how does an abstract class instantiate?
is instantiated by a specific subclass in a polymorphic manner. In fact, this is a polymorphic, abstract polymorphism.

D: Subclass of abstract class
or an abstract class .
Either rewrite all the abstract methods in the abstract class

C: Case Demo
Abstract class features

09.10_ Object-oriented (member characteristics of abstract classes)

A: Member characteristics of abstract classes
A: Member variable: it can be either a variable or a constant. Can abstract modify member variables? member variables cannot be decorated
B: Construction method: Yes.
Initializes the child class to access the parent class data.
C: Member method: It can be either abstract or non-abstract.
B: Case Demo
Member characteristics of abstract classes

C: Member method attribute of abstract class:
A: The abstract method enforces what subclasses do.
B: The non-abstract method subclass inherits things, improves the code reusability.

09.11_ Object-oriented (Sunflower treasure)
Case Demo
The role of abstract classes

09.12_ Object-oriented (abstract class practice cat and dog case)
A: Case Demo
Specific things: cats, dogs
Commonness: Name, age, meal
Cat's character: Catching mice
Characteristics of dogs: housekeeping

09.13_ Object-oriented (abstract class practice teacher case)
A: Case Demo
Specific things: Basic class teacher, employment class teacher
Generality: Name, age, lecture.
Specific things: Basic class students, employment class students
Commonness: Name, age, learning

09.14_ Object-oriented (abstract class practice employee case)
A: Case Demo
If we need to design a programmer class when we develop a system, the programmer contains 3 attributes: Name, work number, and salary.
Manager, in addition to the attributes that contain the programmer, there is also a bonus attribute.
Use the idea of inheritance to design programmer classes and manager classes. Requires that the class provide the necessary methods for property access.

09.15_ Object-oriented (interview questions in abstract class)

A: Face question 1
can an abstract class be defined as an abstract class if there is no abstract method? If so, what's the point?
OK
there is only one purpose, that is, to not allow other classes to create objects of this class, to complete the child class
B: Face question 2
Abstract cannot coexist with which keywords
Static, Final, private

Explain:
Abstract and static
There is no method body for the method that is modified by abstract
The method to be modified by static is to use the class name. Called, but the class name. Calling the re-election ing method is meaningless;
Abstract and final
The method that is modified by the abstract is forced to be overridden by subclasses
The final modifier does not let subclasses rewrite, they are contradictory.
Abstract and private
The method that is modified by the abstract is to let the subclass see and force the rewrite.
The private decoration does not let the sub-class access, they are contradictory.

abstract class Demo{    publicstaticabstractvoidprint();    //错误,非法的修饰符组合    publicfinalabstractvoidprint();    //错误,非法的修饰符组合    privateabstractvoidprint();    //错误,非法的修饰符组合}
09.16_ Object-oriented (overview of interfaces and their characteristics)

A: Interface Overview
From a narrow point of view, it means interface in Java.
From a broad perspective, the external rules are all interfaces.
B: Interface Features
A: interface with keyword interface representation
Interface interface Name {}
B: Class implementation interface with implements representation
Class name implements interface name {}
C: interface cannot be instantiated
So, how does an interface instantiate?
To instantiate in a polymorphic manner.

Interfacenew Demo();i.print();

D: Sub-class of the interface
A: It can be an abstract class. But it doesn't make much sense.
B: Can be a specific class. to override all abstract methods in an interface . (Recommended scheme)
C: Case Demo
Interface Features

09.17_ Object-Oriented (interface member features)

A: interface member characteristics
member variables , which can only be constants and are static and public.
* Default modifier: public static final * Three key order unordered
* Suggestion: Give yourself manually.

Constructor Method: The interface does not have a constructor method.
The subclass constructor is the constructor that accesses the object objects by default.
A class that does not write inherits any class and inherits the object class by default

member Method: can only be an abstract method. That is, the method cannot have a subject.
* Default modifier: Public abstract *
* Suggestion: Give yourself manually.
B: Case Demo
Interface member Features

09.18_ Object-oriented (classes and classes, classes and interfaces, interfaces and interfaces)

A: Classes and classes, classes and interfaces, interface and interface relationships
A: Classes and classes:
Inheritance, can only be single-inheritance, multi-level inheritance.
B: Class and Interface:
The implementation of the relationship, can be implemented alone, or more.
You can also implement multiple interfaces while inheriting a class.

class class1 extends class2 implements InterA,InterB{    。。。}

C: Interface and Interface:
inheritance, which can be either single-inheritance or multiple-inheritance.

interface InterC extends InterA,InterB{    。。。}

B: Case Demo
Class-To-Class, class-to-interface, interface-to-interface relationships

09.19_ Object-oriented (the difference between abstract classes and interfaces)

A: Member Differences

Abstract class:
Member variables: Can be variables, or constants
Construction method: Available
Member methods: can be abstract or non-abstract
Interface:
Member variable: can only be constant
Member methods: can only be abstracted
B: Relationship Difference
Classes and Classes
Inheritance, single inheritance
Classes and Interfaces
Implementation, single implementation, multi-implementation
Interfaces and Interfaces
Inheritance, single inheritance, multiple inheritance

C: Differences in design concepts
Abstract Classes are inherited as follows: "is a" relationship. The common function of the inheritance system is defined in the abstract class.
The interface is implemented as follows: "Like a" relationship. The extension functionality of the inheritance system is defined in the interface.

09.20_ Object-oriented (cat and dog cases join the high jump function analysis and code implementation)
A: Case Demo
Animal type: Name, age, eat, sleep.
The Cat and the dog
Animal training interface: High jump

 Packageday09_polymorphic; Public  class demo3_interface {     Public Static void Main(string[] args) {Catdemo c =NewCatdemo ();        C.eat ();        C.sleep (); Jumpcat JC =NewJumpcat ();        Jc.eat ();        Jc.sleep ();    Jc.jump (); }}AbstractClass animaldemo{//Common: eating, sleeping    PrivateString name;Private intAge Public Animaldemo(String name,intAge) {Super(); This. name = name; This. Age = Age; } Public Animaldemo() {Super(); } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } Public Abstract void Eat(); Public Abstract void Sleep();} Interface jump{//Extension features: High jump     Public Abstract void  Jump();} Class Catdemo extends animaldemo{ Public Catdemo(){Super(); } Public Catdemo(String name,intAge) {Super(Name,age); }@Override     Public void Eat() {System.out.println ("Cat eats fish"); }@Override     Public void Sleep() {System.out.println ("Cat sleeps."); }}class Jumpcat extends Catdemo implements jump{ Public Jumpcat(){Super(); } Public Jumpcat(String name,intAge) {Super(Name,age); }@Override     Public void  Jump() {System.out.println ("Cat high Jump"); }}

09.21_DAY09 Summary
Summarize today's knowledge points once again.

My Summary:
Abstract classes can have abstract methods, which can have no abstract methods. Abstract classes have abstract methods, and classes with abstract methods must be abstract classes. subclass inherits from the abstract class, the subclass must implement all abstract methods in the abstract class.
All methods in an interface are abstract methods. Class is an abstract method, it is defined as an interface on the line. class implements the interface, it is also important to implement all methods of the interface.

Object-oriented _ multi-state _ abstract Class _ Interface

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.