Java how to program (Third Edition) -- My java notes (4)

Source: Internet
Author: User
Tags constructor inheritance
Chapter 9 Object-oriented programming
1. java does not support multi-inheritance (as in C ++), but it supports the "interface" concept. The interface enables java to achieve many advantages of multi-inheritance and discards the corresponding disadvantages.
2. sub-classes can access the public, protected, and package access members of their superclasses (that is, members with no permission modifier and in the same package as their superclasses ), if a superclass does not allow some of its members to be accessed by subclass generated by inheritance, it must declare the member in private mode. The public and protected members of the super class will become the public and protected members of the subclass. A subclass cannot inherit a superclass constructor, but a subclass constructor can call a superclass through a super reference.
3. The protection for access to a protected member is between the protection for access to a public member and the protection for access to a private member. Only the superclass method, subclass method, and other classes in the same package can access the super class protected member (the protected member can access the package ).
4. Instance analysis
1) source code
// Define point2.java for the super-class Point2
Package Point;
Public class Point2 {
Protected int x, y;
// No-argument constructor, mast be encoded in the superclass, or will incur the compile error;
Public Point2 (){
SetPoint (0, 0 );
  }
// Constructor
Public Point2 (int a, int B)
{SetPoint (a, B );}
Public void setPoint (int a, int B)
{X = a; y = B ;}
Public int getX ()
{Return x ;}
Public int getY ()
{Return y ;}
Public String toString ()
{Return "[" + x + "," + y + "]";}
}
// Define the subclass Circle2; circle2.java
Package Point;
Public class Circle2 extends Point2 {
Protected double radius;
// No-argument constructor
Public Circle2 (){
SetRadius (0 );
  }
// Constructor
Public Circle2 (double r, int a, int B ){
Super (a, B); // explict call to superclass constructor;
SetRadius (r );
  }
Public void setRadius (double r)
{Radius = (r> = 0.0? R: 0.0 );}
Public double getRadius () {return radius ;}

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.