The @override in Java

Source: Internet
Author: User

In the Java inheritance mechanism, subclasses can overwrite methods in the parent class.

Base.java

public class Base {public
	base () {
		System.out.println ("base");
	}
	
	public void Foo () {
		System.out.println (' foo in Father ');
	}

Checket.java:
public class Checket extends Base {public
	checket () {
		super ();
		System.out.println ("Checket");
	}
	
	@Override public
	void foo () {
		System.out.println (' foo in Son ');
	}

Mainc.java:

public class Mainc {public
	static void Main (String [] args) {
		checket c = new Checket ();
		C.foo ();
	}


The Foo function in Checket.java the Foo method in the parent base, and the @override keyword (which I have understood as a keyword) is to tell the compiler that if Foo has this method in the parent class, the compilation passes if it does not exist in the parent class. Then there will be an error:

Thread [main] (Suspended (the method foo () of type Checket must override or implement a Supertype method))
Checket.foo () Line:9
Mainc.main (string[]) line:5

The existence here, refers to the method name, parameters, should be consistent.

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.