Java abstract, polymorphic

Source: Internet
Author: User

 1.Java abstract class

1. Related concepts of abstract classes

If a class does not contain enough information to depict a specific object, such a class is an abstract class.

such as shape classes are abstract classes, circles, triangles, etc. are concrete classes.

Classes that are decorated with abstract are abstract classes. If a class contains an abstract method, the class must be defined as an abstract class. Abstract classes, however, do not necessarily have abstract methods.

2. classes that are decorated with abstract are abstract classes. If a class contains an abstract method, the class must be defined as an abstract class.

Abstract classes can have member properties and non-abstract member methods.

Abstract classes cannot be instantiated, but they can have constructors.

An abstract class can only be used as a base class, representing an inheritance relationship. A non-abstract class that inherits an abstract class must implement all of its abstract methods, and the parameters and return values of the implementation method are the same as in the abstract class. Otherwise, the class must also be declared as an abstract class.

3. Use the keyword abstract to define an abstraction class, general syntax:

[ access rights ] abstract class class name { member list }

4. Considerations for Abstract Classes

Abstract classes can have a construction method, but not directly instantiated, can only be used to inherit;

Derived subclasses of an abstract class should provide a concrete implementation of all of its abstract methods, and if the derived subclass of the abstract class does not implement all of the abstract methods, the derived subclass is still an abstract class and can only be used for inheritance, not instantiation;

Abstract classes can also contain non-abstract methods, when overriding non-abstract methods in subclasses, the return value and parameters must be consistent with the parent class;

construction methods and static methods cannot be decorated as abstract.

2.Java Abstraction method

1. Concepts related to abstract methods

In some cases, the class cannot (or does not necessarily) provide a concrete implementation of the method, which can be declared as an abstract method;

The method in which there is no method body in a class is an abstract method;

Classes that contain abstract methods must be abstract classes.

2. Use the keyword abstract to declare an abstraction method, general syntax:

[ access rights ] abstract return value type method name ( parameter list );

3.the role of abstraction

1. in the object-oriented realm, abstract classes are primarily used for type concealment, which is the use of abstract types for programming, but concrete types can be used at runtime.

Using abstract concepts, you can create an extensible architecture and optimize your programs in your development projects.

Abstract classes, abstract methods, are design-level concepts in the software development process. In other words, designers will design abstract classes, abstract methods, programmers are to inherit these abstract classes and override the abstract method, to achieve specific functions.

4. Polymorphism

1. polymorphism is one of the three main characteristics of object-oriented

The meaning of polymorphism is: a form of external expression, there are many concrete realization.

the specific embodiment of polymorphism in Java

Method overloading

Method overrides

Polymorphic parameters (new content, emphasis, difficulty)

5. run-time type and compile-time type

1. first case: The compile period type is the same as the run-time type

A = new A ();

A.show ();

Statement A, the type a used to represent the type of the object, known as the compilation period type , at compile time, the virtual machine considers the type of a is A , for a The validity of the properties and methods used will go to the class A to verify.

Construction Method the A In a () is the run-time type , and the method in the run-time type is executed at run time.

2. The second case: when there is an inheritance relationship, there may be situations where the compile-time type and the run-time type are different, that is, the compile-time type is the parent class type, and the run-time type is the subclass type.

Class A is The parent class of class B

A ab = new B ();

Ab.show ();

The compilation period for Object AB is the parent class A class, and the run-time type is subclass class B .

If a reference has a different compile-time type and a run-time type, then it must be a parent subclass relationship between the compile-time type and the run-time type.

The method used by the AB object is verified in the compile period to class A , while the run executes the class B method

if the compile-time type of an object is a parent class and the run-time type is a subclass, you can cast the object into a type that is the same as the run-time type, that is, the compiler type. b b= (b) ab;

6.Parent class reference to child class object

1. When the compiler type is a parent class, when the run-time type is a subclass, it is referred to as the parent class reference to the Child class object

public class Father {

void Study () {

This.strxchinaese= " primary language ";

System.out.println (" I am learning " +this.strxchinaese);

}

}

public class Son extends Father {

void Study () {

}

}

public class Main {

public static void Main (string[] args) {

TODO auto-generated Method Stub

Father fa=new Son ("456");

Fa.study ();

}

}

Object Father can call All methods in the Son class,fa can only call methods defined in Father,

the new method of the Son extension cannot be called.

7.calls to properties and methods in polymorphic environments

1. Invocation of member methods in polymorphic environments

public class Father {

void Study () {

This.strxchinaese= " primary language ";

System.out.println (" I am learning " +this.strxchinaese);

}

}

public class Son extends Father {

void Study () {

}

}

public class Main {

public static void Main (string[] args) {

TODO auto-generated Method Stub

Father fa=new Son ("456");

Fa.study ();

}

}

Simply put: Compile to look to the left and run to the right.

2. call to a static member method in a polymorphic environment public class Father {

static void study () {

This.strxchinaese= " primary language ";

System.out.println (" I am learning " +this.strxchinaese);

}

}

public class Son extends Father {

static void study () {

}

}

public class Main {

public static void Main (string[] args) {

TODO auto-generated Method Stub

Father fa=new Son ("456");

Fa.study ();

}

}

Simply put: Compile and run all looking to the left.

3. calls to member variables in polymorphic environments

public class Father {

int a=100;

void Study () {

This.strxchinaese= " primary language ";

System.out.println (" I am learning " +this.strxchinaese);

}

}

public class Son extends Father {

int a=200;

void Study () {

}

}

public class Main {

public static void Main (string[] args) {

TODO auto-generated Method Stub

Father fa=new Son ("456");

Fa.study ();

}

}

Simply put: Compile and run all look to the left of the equals sign.

Note: The variable does not exist in the quilt class overwrite this argument, only method exists overwrite

8.polymorphic Parameters

1. Method parameters are polymorphic

public class Animal {

void Eat () {

System.out.println (" animals eat food ");

}

}

public class Cat extends animal{

void Eat () {

System.out.println (" Cat eats cat food ");

}

}

public class Dog extends animal{

void Eat () {

System.out.println (" Dog eats meat ");

}

}

public class Main1 {

public static void Main (string[] args) {

TODO auto-generated Method Stub

Animal Animal = new Animal ();

Cat cat=new Cat ();

Dog Dog=new Dog ();

/*cat.eat ();

Dog.eat (); */

Method (dog);

Method (CAT);

}

static void Method (Animal Animal) {

Animal.eat ();

}

}

The benefits of the method parameter polymorphism: Increasing the extensibility of the Code

9.Object modeling under multi-state environment

1. Upward Styling – also known as automatic type lifting

public class Father {

void Study () {

This.strxchinaese= " primary language ";

System.out.println (" I am learning " +this.strxchinaese);

}

}

public class Son extends Father {

void Study () {

}

}

public class Main {

public static void Main (string[] args) {

TODO auto-generated Method Stub

Father fa=new Son ("456");

Fa.study ();

}

}

Only methods in the parent class can be used

2. Down Styling – also known as downward transformation

public class Father {

void Study () {

This.strxchinaese= " primary language ";

System.out.println (" I am learning " +this.strxchinaese);

}

}

public class Son extends Father {

void Study () {

}

}

public class Main {

public static void Main (string[] args) {

TODO auto-generated Method Stub

Son son=new Father ("456");

Son.study ();

}

}

Methods in the child parent class can be used

10.instanceof operator

1.instanceof Usage

result = Object name instanceof type

Parameters:

Result: Boolean type.

Object name: Required option, any object expression.

type: Mandatory option, any defined class, can be the class in the API, which can be a custom class.

Description

if the object is an instance of this type, the instanceof operator returns true. Returns False if the object is not an instance of the specified class, or if the object is null

The instanceof operator is used to indicate at run time whether an object is an instance of a particular class

public class Animal {

void Eat () {

System.out.println (" animals eat food ");

}

}

public class Cat extends animal{

void Eat () {

System.out.println (" Cat eats cat food ");

}

}

public static void Main (string[] args) {

TODO auto-generated Method Stub

Animal Animal = new Animal ();

Cat cat=new Cat ();

Dog Dog=new Dog ();

/*cat.eat ();

Dog.eat (); */

Method (dog);

Method (CAT);

}

static void Method (Animal Animal) {

if (animal instanceof Cat) {

Cat c= (CAT) animal;

C.eat ();

}

}

 

Abstract, polymorphic of Java

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.