The in-depth study of class

Source: Internet
Author: User
Tags abstract constructor inheritance tostring stringbuffer

1: Using Inheritance in Java

The most powerful function in object-oriented program design is class inheritance, class inheritance allows you to write a new program on top of an existing class, for example, if you want to create a rectangular class that can be displayed on the screen and populate it, you can start from scratch or take advantage of the old rectangular class, The following sections will show you how to inherit an existing rectangle class without rewriting the code in it.

For example, to create a FillRect class, the class can use all the defined data and member functions in the rectangle class, such as width, height, and other member functions, such as the data and Getarea, but the use of inheritance methods to implement. Using the Extands keyword to allow Java programmers to inherit the member functions of an existing class, in order to inherit the rectangle class, you must reference the old rectangle class, you must refer to the old rectangle class, and refer to it in the description of the new class, such as:

import Shapes.Rectangle;
class fillRect extands Rectangle
{
.....
}

2: Overloading of member functions

After inheriting, how to make FillRect analogy rectangle class to improve? We can implement a new DrawRect member function with the following code that will greatly shorten the code and fill the rectangle instead of just drawing the outline of the rectangle:

private String makeString(chr ch,int num)
{
StringBuffer str=new StringBuffer();
for(int i=num;i>0;i--)
str.append(ch);
return str.toString();
}
public void drawRect()
{
for(int i=height;i>0;i--)
System.out.println(makeString("#",width));
}

Note that we use the StringBuffer class here. StringBuffer is used because a string can only produce a static type-its size cannot be changed, and StringBuffer can produce a variable-length string type.

Here, the DrawRect member function is overloaded, and by using the same member function name, you can replace the old member function with the new member function. However, member functions that are described as final cannot be overloaded.

Note that you don't have to include the same code as the inherited class in the new class, but just add what you want, but you have to create a new constructor member function to differentiate between the two different classes.

The full picture of the new class is as follows, and you can see that by inheriting the rectangle class, the code becomes very simple and straightforward.

class fillRect extands Rectangle
{
public fillRect(int w,int h)
{
supper(w,h);
private String makeString(char ch,int num)
{
StringBuffer str=new StringBuffer();
for(int i=num;i>0;i--)
str.append(ch);
return str.toString();
}
public void drawRect()
{
for(int i=height;i>0;i--)
System.out.printlm(makeString("#",width));
}
}
}

3: Using the interface

Java can create a class called an interface (interface) in which all member functions are abstract, meaning that they are all only described as undefined, and you can illustrate an interface as follows.

public interface InterfaceName

member function description

The default reference type for member functions in an interface is private, and the internal variables of the interface (interface) are immutable and always static and final.

By using the keyword implement, you can inherit an interface when you define a class. Unlike extends, however, a class can inherit multiple interfaces at the same time.

What are the advantages of using an interface? By creating an interface, you can describe a set of abstract member functions without having to implement it specifically, and all classes that inherit this interface will have a member function with the same prototype. For example, you want all shapes to have a draw () member function, you can create an interface and name it with shape:

public interface Shape

void Draw ();

Now, whenever you create a class that inherits from shape, you will have a member function draw ().

4: Conversion of the class

Class, which is similar to the conversion of different types of variables, but not the same.

We can convert an object of a parent class to a subclass object, and the following code illustrates an example of a class transformation where the Y class inherits from the X class:

Y y=new Y();
X x;
x=y;

Note that the conversion between two subclasses is not possible.

5:null,this and supper variables

All classes have three variables: null, this, and supper.

The null variable does not point to any actual object, but instead points to an empty object, as in the following example:

Rectangle Rect=null;

This example produces a variable of a rectangular class, but does not create an actual object. In addition, if a member function requires an object as an argument, you can also use NULL instead.

The this variable points to the object itself, and a class can use the this variable to obtain an object variable representing itself.

The supper variable is a variable that points to the constructor member function of the class parent class, and you can quickly complete the design of the constructor member function of the subclass by calling it.

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.