Java Super keyword

Source: Internet
Author: User

Subclasses use super to manipulate hidden member variables and methods

Example5_7.java

classSum {intN; floatf () {floatSum=0;  for(inti=1;i<=n;i++) Sum=sum+i; returnsum; }}classAverageextendsSum {intN; floatf () {floatC; Super. n=N; C=Super. f (); returnc/N; }   floatg () {floatC; C=Super. f (); returnC/2; }} Public classExample5_7 { Public Static voidMain (String args[]) {Average aver=NewAverage (); AVER.N=100; floatRESULTTWO=AVER.G ();floatResultone=aver.f (); System.out.println ("Resultone=" +Resultone); System.out.println ("Resulttwo=" +resulttwo); }}

To call the constructor of a parent class using super

Example5_8.java

classStudent {intNumber ;   String name; Student () {} Student (intnumber,string name) {       This. number=Number ;  This. name=name; System.out.println ("My name is:" +name+ "number is:" +Number ); } }classUniverstudentextendsStudent {Booleanmarriage; Universtudent (intNumber,string name,Booleanb) {Super(Number,name); Marriage No=b; System.out.println ("Marriage no =" +marriage); }} Public classExample5_8 { Public Static voidMain (String args[]) {universtudent Zhang=NewUniverstudent (9901, "He Xiaolin",false); }}

Java Super keyword

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.