Java polymorphism abstract class

Source: Internet
Author: User

One 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 implementation

The specific embodiment of polymorphism in Java
Method overloading
Method overrides
Polymorphic parameters (new content, emphasis, difficulty)

Object modeling under multi-state environment

Down styling – also known as downward transformation

Upward styling – also called upward transformation

Example: (It contains polymorphic object modeling, etc.)

public class Aabb {

public static void Main (string[] args) {

Dog Adog=new Dog ();
ADOG.EAT1 ();
Adog.jiao ();
Animal animal=new Dog ();
Dog x= (dog) animal;
X.play ();
Animal a= new Pig ();
Pig y= (pig) A;
Y.play ();
Y.EAT2 ();
Cat cat=new Cat ();
Cat.he ();
Cat.eat ();
Animal B = new Cat ();
B.eata ();

}

}
Class Animal {

void Play () {
System.out.println ("The cat is playing ball balls");


} void Eata () {
System.out.println ("Animal eats meat");
}


}

Class Cat extends animal{

void Eat () {
Super.eata ();
System.out.println ("The cat drank and was hungry, then jumped into the river to catch the fish, unfortunately it was drowned");
}
void He () {
System.out.println ("Then the cat runs to the river to drink Water");
}
}


Class Dog extends animal{
void Eat1 () {
System.out.println ("The dog is Hungry barking");

}void Jiao () {
System.out.println ("Find bones after eating bones");
}
}

Class Pig extends animal{
void Eat2 () {
System.out.println ("A pig in the side of wanton grazing");
}
}
Two abstract 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.
The keyword abstract defines an abstraction class, a general syntax:
[Access rights] abstract class name {
Member List
}
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.
The keyword abstract declares an abstraction method, a general syntax:
[Access rights] Abstract return value type method name (parameter list);
The role of abstraction
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.
The three Java interfaces are declarations of a series of methods and are abstract collections
Basic format of the interface
[Modifier] Interface Interface Name [extends parent interface List]{

[Public] [Static] [Final] constant;

[Public] [Abstract] method;
}
Interface Name: A required parameter that specifies the name of the interface and the interface name must be a valid Java identifier. In general, the first letter is required to capitalize.
Extends parent interface Name list: Optional parameter that specifies which parent interface the interface to define inherits from. When you use the extends keyword, the parent interface name is a required parameter. The parent interface can exist multiple, separated by commas.
Method: The method in the interface is only defined and not implemented
The Java interface inherits the principles of the interface:
Java interfaces can inherit multiple interfaces
The interface inheritance interface still uses the keyword extends
Interface inheritance contrasts with class inheritance: The inheritance of Java classes is a single inheritance, and the inheritance of Java interfaces is multiple inheritance.
class implements the basic format of the interface:
[Modifier] Class < class name > [extends parent class name] [Implements interface list]{

}

Java polymorphism abstract class

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.