The instanceof keyword in Java

Source: Internet
Author: User

instanceof is a keyword in Java that determines whether an object running is an instance of a particular class, INSTANCEOF returns a Boolean if the object is an instance of a particular class, returns True, and vice versa.

Class Instanceofdemo {public static void main (string[] args) {Cfather C1 = new CChild1 ();//Parent object reference points to subclass Object Test (C1); Cfather C2 = new CChild2 (); test (C2);} public static void Test (Cfather c) {c.print ();                Use instanceof to determine if C is an instance of CChild2 if (c instanceof CChild2) {CChild2 C2 = (CChild2) c;c2.print1 ();}}} Abstract class Cfather{abstract public void print ();//Abstraction Method}class CChild1 extends cfather{//public void print () { System.out.println ("This is Child1");}} Class CChild2 extends cfather{public void print () {System.out.println ("This is Child2");} Other methods of this class public void Print1 () {System.out.println ("Hello java");}}    

  

The instanceof keyword in Java

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.