----polymorphism and exception handling in the Java language

Source: Internet
Author: User

DAY13----polymorphism and exception handling in the Java language

First, overview:

Learning the Java language requires understanding the four main features of object-oriented in Java: Encapsulation, abstraction, inheritance, polymorphism. The first three we have finished learning, let's learn polymorphism. What is polymorphic? The Unknown Incredibles is two kinds of morphology, and one method can define two forms, which is the polymorphism of the method. At the same time we will learn what is method overloading, and method signing.

Exception handling is to compile the time we need to manually deal with the compiler can not pass the program. The exception can be handled using the try {} catch{} structure.


Two, polymorphic:

1. The base class of all classes is object, and the class is inherited by default when creating a class.

2, method overloading: A class inside the same method is defined multiple times, is a static polymorphism of the embodiment.

3, method signature: Through the method of the parameter part is a method signature, according to the method signature to distinguish. such as "sum (int a,int b)"

Note: when the return type of the method is different, it is not part of the method's overload.

Method overloading: Is the embodiment of static polymorphism. At the same time, in the process of compiling the program to decide which form to call

Method rewriting: Is the embodiment of dynamic polymorphism. It is also the process of running the program to decide which pattern to call

4. Example One

The static polymorphic method defined by class student{//public static void sum (int a,int b) {System.out.println (a+b);} public static void sum (float a,float b) {System.out.println (a+b);} public static void sum (int a,float b) {System.out.println (a+b);} public static void sum (float a,int b) {System.out.println (a+b);}} public class Polymorhism01 {public static void main (string[] args) {//Because it is a static method, it can be called directly from the class name. Student.sum (2, 3); Student.sum (2f, 3f); Student.sum (2f, 3); Student.sum (2, 3f);}}

Third, exception handling:

1. Use the try {} catch{} finally{} structure.

2, compile-time exception: refers to the compile time back automatically prompts the compilation will not pass.

Run-time exception: Indicates that an exception occurred while running. A non-normal terminal appears.

3. Example:

public class Exception01 {public static void main (string[] args) {///instance: two-digit quotient//try module is a block of code that handles possible exceptions, tries to catch exceptions try {//Accept Console input Scanner sc = new Scanner (system.in); System.out.println ("Enter the first number! "); int a = Sc.nextint (); System.out.println ("Enter a second number! "); int b = Sc.nextint (); System.out.println (A/b);//catch module refers to how to handle caught exceptions} catch (Exception e) {//catch all exceptions! SYSTEM.OUT.PRINTLN ("Divisor cannot be zero!") ");//e.printstacktrace (); This is the print exception message. The finally module is the release of resources after the program is finished. Whether or not an exception occurs, you can execute}FINALLY{SYSTEM.OUT.PRINTLN ("program End! ");}    }}

4. Throw an exception using the Throw keyword

Note: Whoever calls a method that has an exception will throw it to someone who can handle the exception

public class Exception02 {//1, throwing exceptions with the throw keyword, who calls on who handles public static void one () {try {System.out.println (10/0);} catch ( Exception e) {Throw e;//throws an exception object e}}public static void-one () {try {one ()} catch (Exception e) {System.out.println ("the one I called, I'll deal with this anomaly! ");}} 2. Throw an exception using the Throw keyword. In this method is dealt with. When an exception is handled. The current exception output can be printed processing, the amount can be used after the method of the//throw keyword plus the relevant exception information to resolve. public static void Student () throws Classnotfoundexception{class.forname ("Com.c3.Student"); public static void Main (string[] args) {//class's Forname method is to manually load a class. He will automatically prompt for abnormalities. try {//Load a class Class.forName ("Com.c3.Student");} catch (ClassNotFoundException e) {System.out.println ("This class may not exist!") ");}}}

Iv. Concluding remarks:

At this point, the core knowledge of polymorphism and exception handling is over, and when you really understand these things you will understand that Java is a wonderful subject.

This article from the "Program Ape" blog, reproduced please contact the author!

----polymorphism and exception handling in the Java language

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.