à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday 8 October 2017

OOPS in JAVA 18


 OOPS in JAVA 18

instanceof Operator

The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).

The instanceof in java is also known as type comparison operator because it compares the instance with type. instanceof operator return boolean value, either true or false.

Example

public class Test
{
    public static void main(String[] args)
    {
      Test t= new Test();
      System.out.println(t instanceof Test);
      }
}
Output : true

Example:

class Animal{}  
class Dog1 extends Animal{//Dog inherits Animal  
  
 public static void main(String args[]){  
 Dog1 d=new Dog1();  
 System.out.println(d instanceof Animal);//true  
 }  
}  
Output: true

Downcasting with java instanceof operator

When Subclass type refers to the object of Parent class, it is known as downcasting. If we perform it directly, compiler gives Compilation error.

Dog d=new Animal();//Compilation error

If you perform it by typecasting, ClassCastException is thrown at runtime.

Dog d=(Dog)new Animal();
//Compiles successfully but ClassCastException is thrown at runtime.

But if we use instanceof operator, downcasting is possible 

Example : Downcasting with instanceof

class Animal {

 }  
  
class Dog extends Animal {  
  static void method(Animal a) {  
    if(a instanceof Dog){  
       Dog d=(Dog)a;//downcasting  
       System.out.println("ok downcasting performed");  
    }  
  }  
   
  public static void main (String [] args) {  
    Animal a=new Dog();  
    Dog.method(a);  
  }  
    
 }  
Output: ok downcasting performed

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website