We value your privacy
We use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking "Accept All", you consent to our use of cookies.
We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.
The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site....
Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.
No cookies to display.
Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.
No cookies to display.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.
No cookies to display.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
No cookies to display.
Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.
No cookies to display.
Published on Jan 03, 2024
Hello! Today, on Tech Bites, we’ll be delving into the instance of operator in Java.
The example of the operator in Java serves the purpose of checking whether an object belongs to a specific class.
Firstly, we will declare a class named “Car” and an interface called “Vehicle.” Additionally, we will create another class named “Showroom.”(1-5)
Moving on to our main method,
we will instantiate an object of the Car class using the statement ‘Car car = new Car();’. This will create a new instance of the Car class. (9-11)
Subsequently, we will employ the ‘instance of’ keyword to check if the ‘car’ object is an instance of the Vehicle interface, using the condition: if (car instance of Vehicle). (13)
Based on the result of this
For the ‘instanceof’ check, the program will print a message. If the condition is true, it will print ‘Car is an instance of Vehicle’. Otherwise, it will print ‘Car is not an instance of Vehicle’.(14 =16)
Upon running the code
it will initially print ‘Car is not an instance of Vehicle.’ To change this outcome, we will implement the Vehicle interface within the Car class, ensuring that it implements all methods declared in the Vehicle interface. (run)
Upon rerunning the code, the output will indicate, ‘Car is an instance of Vehicle.’
Thank you for exploring this topic with us today. Remember to subscribe for more insightful videos.
Thank you for joining us today. Stay tuned for more videos.