Summary of Inheritance

Source: Internet
Author: User
Summary of Inheritance

1. Benefits of Inheritance:

1. If there is a lot of code in the subclass and the parent class, you can use inheritance to reduce redundancy by much less code.

2. Inheritance is used to perfect object-oriented programming ideas.

2. Inherited formats:

Access modifier class Subclass name extends parent class name {}

3. Subclasses inherit all the properties and methods of the parent class, but the child class calls the parent class's method when the score condition

Subclass and Parent class in the same package

Subclasses can invoke all properties and methods except private for the parent class;

Subclasses and parent classes are no longer in the same package

Subclasses can only invoke properties and methods common to the parent class

4. Rewrite

The meaning of rewriting: the behavior of classes with inheritance in real life can be different, and rewriting is the imitation of this phenomenon.

Overridden condition: 1. To implement overrides, it must be a method that already exists in the parent class;

2. The overridden method of a subclass must be the return value type, the number of arguments, the parameter of the corresponding method of the parent class.

Type, the method name is exactly the same.

Overriding the only type that can be different from the parent class is the access modifier, but the subclass overrides the method

The access modifier cannot be smaller than the parent class.

Access modifiers from large to small public---àprotected--àprivate

The difference between overrides and overloads:

1. Overloading the same class, overriding is between the parent class and the child class

2. Overloading is a method with the same method name, which returns a different value type or parameter type or number. Overrides are the method return value type of the subclass and the parent class, the parameter type, the number of arguments, and the method names exactly.

The realization of automatic transformation

Automatic transformation can only exist between the parent class and the subclass, and the implementation of automatic transformation must have an inheritance relationship.

Auto-transform format one:

Parent class Name Object name = new Subclass construction method ();

The object that is automatically transformed is the object of the subclass, but automatic transformation cannot refer to the method defined by the subclass itself.

This flaw can be overcome by forced transformation. Format for forced transformation:

Subclass name New Object name = (subclass name) Original object name

Automatic Transformation Format two:

Subclass name Sub-class object name = new Subclass construction method ();

Access modifier return value type method Name (parent class Name parent class object name) {}

Method Name (subclass name Sub-class object name) {}

Auto-transition conditions: Automatic transformation The referenced method must be the overridden method.

Polymorphism: Polymorphism is caused by overloading of methods, rewriting, inheritance, and automatic transformation resulting in indeterminate results.

Data type: The data type has two major classes, the base data type and the class data type (reference data type).

Basic data type:

byte byte type -128--127

char character type 16bit

int integral type 32bit

Short Quick-integer 16bit

Float Single Precision 32bit

Long-length integer 64bit

Double Dual Precision 64bit

Boolean Boolean True or False

Value passing of the base data type

Reference data types: As long as the defined classes are reference data types, abstract class and interface are also reference data types, and arrays are reference data types.

int [] I = new INT[10];

Value passing

Value passing applies to both the base data type and the String class, (the string class can also be passed with reference data), allocating stack memory

Reference passing applies To reference data types. Open up stack memory and heap memory.

Perfect Login Interface (add image)

1. There are two ways to get the picture path

The first type: String url = "Picture path"; The path is divided into relative path and absolute path, and relative path is generally used.

The second type: String.net.URL URL = this. Class (). GetResource ("picture name"); This is the path to the picture used for the current class, for pictures and classes that exist under the same package.

2. Instantiate a picture object, using Javax.swing.ImageIcon ();

3. Instantiate an image interface and add the image interface to the container.

  • 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.