Introduction to the application of _java based on Java Neutron class inheritance

Source: Internet
Author: User
Tags inheritance instance method

1, the definition of inheritance

A subset of the members of a subclass is defined by its own declaration, and the other part is inherited from his parent class. The subclass inherits the member variables of the parent class as one of its own member variables, as if they were directly in the subclass

As stated, you can manipulate any instance method of your own in the quilt class. That is, a subclass inherits a member that is a fully-meaningful member of the class, and if the instance method declared in the subclass cannot manipulate the parent class

A member variable that does not inherit from the quilt class, and the subclass inherits the method of the parent class as a method in the subclass, as if they were directly sound in a subclass, and you can declare a tiger instance in the quilt class

Method call.

2. Subclass parent class in a package

Subclasses can inherit member variables and methods in the parent class except private, as their own member variables and methods. The access rights of inherited member variables and methods are invariant.

/test/src/com/b510/parent.java

Copy Code code as follows:

Package com.b510;

/**
* @author Hongten
*
* @time 2012-3-13 2012
*/
public class Parent {
private int numbera = 10;
protected int numberb = 20;

/**
* Friend function sum ()
*/
void sum () {
Numberb = Numbera + numberb;
System.out.println (Numberb);
}

/**
* Get Numbera, this method is private
*
* @return Numbera
*/
private int Getnumbera () {
System.out.println (Numbera);
return Numbera;
}

/**
* This method is of public type
*/
public void print () {
System.out.println ("numberb+numbera=" + (Numberb + Numbera));
}

/**
* This method is protected type of
*/
protected void Say () {
System.out.println ("Hello,i ' m parent class!");
}
}

/test/src/com/b510/parentandsubclass.java
Copy Code code as follows:

Package com.b510;

/**
* @author Hongten
*
* @time 2012-3-13 2012
*/
public class Parentandsubclass {
public static void Main (string[] args) {
Subclass sub = new Subclass ();
Inheriting methods in the parent class
Sub.say ();
Sub.sum ();
Sub.print ();
Subclasses own methods
Sub.profun ();
Sub.youyuan ();
}

}

/test/src/com/b510/subclass.java
Copy Code code as follows:

Package com.b510;

/**
* @author Hongten
*
* @time 2012-3-13 2012
*/
public class Subclass extends Parent {

/**
* Friend method
*/
void Youyuan () {
SYSTEM.OUT.PRINTLN ("Subclasses cannot inherit Numbera, but can inherit numberb=" + Numberb);
System.out.println ("This is a friend method in subclasses");
}

/**
* Private Method
*/
private void Prifun () {
System.out.println ("This is Private method");
}

/**
* Protected method
*/
protected void Profun () {
System.out.println ("numberb=" + Numberb can be inherited in subclasses);
}

}

Run Result:
Copy Code code as follows:

Hello,i ' m parent class!
30
Numberb+numbera=40
Numberb=30 can be inherited in subclasses
Subclasses cannot inherit Numbera, but can inherit numberb=30
This is the friend method in the subclass

Summary: When subclasses and parent classes are in the same package, subclasses cannot inherit the private variables and methods of the parent class.

3. Subclasses and parent classes are not in the same package
/test/src/com/parent.java

Copy Code code as follows:

Package com;

/**
* @author Hongten
*
* @time 2012-3-13 2012
*/
public class Parent {
private int numbera = 10;
protected int numberb = 20;

/**
* Friend function sum ()
*/
void sum () {
Numberb = Numbera + numberb;
System.out.println (Numberb);
}

/**
* Get Numbera, this method is private
*
* @return Numbera
*/
private int Getnumbera () {
System.out.println (Numbera);
return Numbera;
}

/**
* This method is of public type
*/
public void print () {
System.out.println ("numberb+numbera=" + (Numberb + Numbera));
}

/**
* This method is protected type of
*/
protected void Say () {
System.out.println ("Hello,i ' m parent class!");
}
}

/test/src/com/b510/parentandsubclass.java
Copy Code code as follows:

Package com.b510;

/**
* @author Hongten
*
* @time 2012-3-13 2012
*/
public class Parentandsubclass {
public static void Main (string[] args) {
Subclass sub = new Subclass ();
Inheriting methods in the parent class
Sub.say ();
Sub.print ();
Subclasses own methods
Sub.profun ();
Sub.youyuan ();
}

}

/test/src/com/b510/subclass.java
Copy Code code as follows:

Package com.b510;

Import com. Parent;

/**
* @author Hongten
*
* @time 2012-3-13 2012
*/
public class Subclass extends Parent {

/**
* Friend method
*/
void Youyuan () {
SYSTEM.OUT.PRINTLN ("Subclasses cannot inherit Numbera, but can inherit numberb=" + Numberb);
System.out.println ("This is a friend method in subclasses");
}

/**
* Private Method
*/
private void Prifun () {
System.out.println ("This is Private method");
}

/**
* Protected method
*/
protected void Profun () {
System.out.println ("numberb=" + Numberb can be inherited in subclasses);
}

@Override
protected void Say () {
System.out.println ("This is the protected method of overriding the parent class, say (), and can also get numberb=" + numberb);
}
}

Run Result:
Copy Code code as follows:

1 This is the protected method of overriding the parent class, say (), and can also get numberb=20
2 numberb+numbera=30
3 You can inherit numberb=20 from a subclass
4 subclasses cannot inherit Numbera, but can inherit numberb=20
5 This is the friend method in the subclass

Summary: Subclasses and parent classes are not in the same package, subclasses can inherit protected,public variables and methods, the access rights of inherited members or methods are unchanged, but subclasses cannot inherit the friendly (friend) variables and friendly (friend) methods of the parent 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.