Google+

212. Default Package







Classes in Java need to be created under any packages. In case, if we don't provide any package name while creating a Class, Java automatically creates the Class under a package called 'Default Package'.

Lets create a Class in a Java Project without creating a Package prior to it and find out what Java does.

Lets implement on Eclipse IDE -

1. Launch Eclipse IDE , Create a new Java Project 'Project 40' and observe that no packages are displayed before creating Class as shown below -


2. Now create a Class 'ClassA' without creating a package prior to it, Java creates the Class under the default package as shown below -


 Hence Java automatically creates the package 'default package' when Class is created without creating any package.





Please comment below to feedback or ask questions.

Creating Hierarchy of Packages will be explained in the next post.







211. Creating duplicate Classes in same project







Pre-requisites -


Can we create duplicate Classes in Same Java Project ?

No, Java gives compiler errors when you create duplicate classes (i.e. Classes with same names).

 Is there any solution to create duplicate Classes in the Same Java Project ?

Yes, we can create duplicate classes in the same Java Project by separating the duplicate classes using Packages.

Lets implement this concept on Eclipse IDE

1. Create a new Java Project 'Project 39' as shown below -



2. Create a Class with name 'ClassA' as shown below -


3. Try to create another Class with same name 'ClassA' and observe that an error is displayed and am unable to create the Class with the same name as shown below -



Hence we cant create duplicate Classes in a single Java Project. Now lets resolve this issue by separating the Duplicate Classes using Packages by following the below steps.

4. ClassA Class created in Step2 was created under a default package. Now lets create another package 'packageD' in the same Java Project 'Project 39' as shown below -


5. Now create a Class with same name ClassA under the newly created package 'packageD' and observe that no error is displayed and the Duplicate Class is successfully created as shown below -


Hence we can create duplicate classes, when we create separate the Classes using packages.






Please comment below to feedback or ask questions.

Default Package will be explained in the next post.




210. instanceof operator








instanceof is a binary operator used to verify whether the given object is an instance or sub-type of the given Class. instanceof operator returns true if the given object is an instance or sub-type of the given Class.

Practice all the below given examples to understand the usage and capability of instanceof operator.

Example 1 -

//Object of a Class is an instance of its Class.

ClassOne object1 = new ClassOne( );

if(object1 instanceof ClassOne)
  System.out.println("object1 is an instance of ClassOne");
else
  System.out.println("object1 is not an instance of ClassOne");

Output -

object1 is an instance of ClassOne.

Lets implement the Example 1 on Eclipse IDE -

1. Create a new Java Project 'Project 38' as shown below -


2. Create a new Java Class file 'ClassA.java' with main( ) method as shown below -


3. Create a Class 'ClassOne' in the same 'ClassA.java' file as shown below -


4. Create an object for 'ClassOne' class in 'ClassA' class as shown below -


5. Use instanceof operator to findout whether the object 'object1' is an instance of 'ClassOne' Class as shown below and use the print statements to print the result to the output -


6. Run the Java Class 'ClassA.java' and observe that the output 'Object1 is an instance of ClassOne Class" is displayed as shown below -



Example 2 -

//Object of Sub Class is an instance of Super Class
//Class Dog is a subclass of Class Animal.

Dog dog = new Dog( );

System.out.println("dog object is an instance of Animal Class is "+ (dog instanceof Animal) );

Output -

dog object is an instance of Animal Class is true

Lets implement the Example 2 on Eclipse IDE -

1. Create a new Java Class 'ClassB.java' with main( ) method in the same Java Project 'Project 38' as shown below -


2. Create two Classes 'Animal' and 'Dog' in the same 'ClassB.java' Class file as shown below -


3. Make Dog Class as a SubClass of Animal Class as shown below -


4. Create an Object for the Sub Class Dog Class as shown below -


5. Now find out whether the sub class object 'dog' is an instance of Super Class 'Animal' using instanceof operator as shown below -


6. Run the Java Class file 'ClassB.java' and verify the result in Output Console as shown below -


Example 3 -

//Object of a Class is an instance of an implemented Class
//Class Pig implements Mammal Interface

Pig pig= new Pig( );

System.out.println("pig object is an instance of Mammal Interface is "+ (pig instanceof Mammal) );

Output -

pig object is an instance of Mammal Interface is true

Lets implement the Example 3 on Eclipse IDE -

1. Create a Java Class file 'ClassC.java' with main( ) method in the same Java Project 'Project 38' as shown below -


2. Create 'Mammal.java' Interface file in the same Project 'Project 38' as shown below -



3. Now go back to 'ClassC.java' Class file and create 'Pig' Class as shown below -



4. Make the Pig Class implement the Interface Mammal as shown below -


5. Create an Object for Pig Class in 'ClassC' Class as shown below -


6. Now use instanceof Operator to find out whether the object of Pig Class is an instance of its implemented Interface Mammal as shown below -


7. Run the Java Class file 'ClassC.java' and observe that the result is displayed in the Console tab as output as shown below -



Example 4 -

Frog frog = null;  //frog object referring to null i.e. no object

System.out.println("frong object is an instance of Frog Class is "+ (frog instanceof Frog) );

Output -

frog object is an instance of Frog Class is false

Lets implement the Example 4 on Eclipse IDE -

1. Create a Class file 'ClassD.java' with main( ) method  in the existing Project 'Project 38' as shown below -

2. Create a Class 'Frog' in the same 'ClassD.java' Class file as shown below -


3. Define an object for Frog Class and assign null reference to the defined object as shown below -


4. Now find out whether the frog object is an instance of Frog Class using the instanceof operator as shown below -


5. Save and Run the Java Class file 'ClassD.java' and observe that the result is displayed in the Console tab as output as shown below -


Hence the object assigned with null reference wont be an instance to its defined Class.

Example 5 -

//After Upcasting, objects of the Super Class and Sub Class are instances of both SuperClass and SubClass
//Use Object Class in this example as it is the Super Class of all the Classes in Java

Monkey monkey = new Monkey( );
Object obj = monkey;

System.out.println("obj object is an instance of Object Class is "+ (obj instanceof Object) );
System.out.println("obj object is an instance of Monkey Class is "+ (obj instanceof Monkey) );
System.out.println("monkey object is an instance of Object Class is "+ (monkey instanceof Object) );
System.out.println("monkey object is an instance of Monkey Class is "+ (monkey instanceof Monkey) );

Output -

obj object is an instance of Object Class is true
obj object is an instance of Monkey Class is true
monkey object is an instance of Object Class is true
monkey object is an instance of Monkey Class is true

Lets implement the Example 3 on Eclipse IDE -

1. Create a Java Class file 'ClassE.java' in the existing Java Project 'Project 38' as shown below -


2. Create a Class 'Monkey' in the same 'ClassE.java' file as shown below -


3. Create an object for Monkey Class in the 'ClassE' class as shown below -



4. Define an object for Object Class and assign the object of Monkey Class to it as shown below -



5. Now find out whether the objects of Monkey Class and its super Class Object are instances of Monkey and Object Classes by writing the following four statements as shown below -


6. Save and Run the Java Class 'ClassE.java' and observe that the results are displayed in the Console tab as output as shown below -


Hence after Upcasting the objects of Sub Class and Super Class are instances of both Sub and Super Classes.

Example 6 -

//Object of Super Class is not an instance of Sub Class
//Assuming Benz is Sub Class and Car is its Super Class

Car car = new Car( );

System.out.println("car object is an instance of Benz Class is "+(car instanceof Benz) );

Output -

car object is an instance of Benz Class is false

Lets implement this Example 6 on Eclipse IDE -

1. Create a Java Class file 'ClassF.java' in the Existing Java Project 'Project 38' as shown below -


2. Create two Classes 'Car' and 'Benz'  in the same 'ClassF.java' Class file as shown below -


3. Make Car Class as a super class for Benz Class as shown below -


4. Create an object for Car Class as shown below -


5. Now find out whether the object of Super Class Car is an instance of Sub Class Benz Class by writing the code as shown below -


6. Save and Run the Java Class file 'ClassF.java' and view the result in the Console tab as output as shown below -


Hence Sub Class cant be an instance for a Super Class object.

Example 7 -

As you already know we can Upcast an object of Sub Class to its just defined Super Class object. After Upcasting, we know that the object of the Super Class will be the instance of Super Class and Sub Class.
We use instanceof operator with the object of Super Class to find whether it is an instance of the Sub Class and downgrade the object of the Super Class to the Sub Class. Object of the Super Class can be downgraded safely without any problems, when the object of its sub class is upgraded and assigned to the just defined Super Class object as shown below -
Cat c1 = new Cat(); 
Animal a = c1; //upcasting to Animal
if(a instanceof Cat){ // testing if the Animal is a Cat
System.out.println("It's a Cat! Now i can safely downcast it to a Cat, without a fear of failure.");
Cat c2 = (Cat)a;
}
The above program when executed , will pass the statement in the if condition and downcast the Super Class object 'a' to Sub Class Cat Class type.

So we have used instanceof operator in the above example to find out whether the object stored in the Animal defined object is a cat. If the object stored in the Animal object a is a cat, then we can easily downcast the cat to a cat without any problems.

Lets assign a dog to the Super Class defined object 'a' as shown below -

Dog d1 = new Dog(); 
Cat c1 = new Cat(); 
Animal a = d1; //upcasting to Animal
if(a instanceof Cat){ // testing if the Animal is a Cat
System.out.println("It's a Cat! Now i can safely downcast it to a Cat, without a fear of failure.");
Cat c2 = (Cat)a;
}
 In the above example, as dog is assigned to the Animal defined object, the if condition fails and the conversion of dog inside Animal object to a cat wont be done.

Hence we can use instanceof operator to find out whether we can downcast an object to any type.






Please comment below to feedback or ask questions.

Creating Duplicate Classes in same Project  will be explained in the next post.









209. Downcasting







While Upcasting (i.e. Assigning Sub Class Object reference to Super Class Object reference) can be automatically done by Java. But Downcasting (i.e. Assigning Super Class Object reference to Sub Class Object reference) cannot be done automatically by Java, but we need to do it manually by following the below syntax -

Assuming that Animal Class is the super Class of the Cat Class -

Cat c1 = new Cat( );  //Creating Object for Cat Class
Animal a = c1; //Automatic Upcasting to Animal
Cat c2 = (Cat) a;  //manually Downcasting back to a Cat

Why Upcasting is automatical and Downcasting need to be done manually ?

Converting a Cat or Dog to Animal can never fail, hence the Upcasting is done automatically. But if you have a group of different animals and cast them all to a Cat, then there are some chances that some of the animals are actually dogs and the process fails. Hence Downcasting need to be done manually to avoid this problem.

After Downcasting, can you run the program sucessfully ?

The answer is Yes and No. The answer is Yes, when you just define the Super Class 'Animal' object instead of creating it. The answer is No, when you create Super Class 'Animal' object. Lets see the below examples to find out the cases -

Example1 - Yes Case - This Program can be Run without any compiler and run time errors. i.e. Dowcasting statement wont throw any errors.

Animal a = null;  //Object a is just define as Animal Class type here but not created.
Cat c2 = (Cat) a;  //Downcasting. This statement wont throw any errors.

Example2 - No Case - This Program wont throw compiler error but throws run time error. i.e. Downcasting statement throws run time error.

Animal a = new Animal( ); //Object created for Animal Class
Cat c2 = (Cat)a; //Downcasting. This statement wont throw compile time error but throws run time error.

Lets implement the Downcasting Objects on Eclipse IDE and find out whether we get any errors by following the below steps -

1. Launch Eclipse IDE, create a new Java Project 'Project 37' as shown below -


2. Create a new Java Class 'ClassOne' with main( ) method as shown below -



3. Create the Classes 'Animal' and 'Cat' Classes in the same 'ClassOne.java' Class file as shown below -


4. Make the 'Cat' Class as the Sub Class of 'Animal' Class as shown below -


5. Now create an object for Animal Class as shown below -


6. Manually Downcast the created Animal object to Cat Class object as shown below -


7. Run the Java Class file 'ClassOne.java' and observe that the Run time error is displayed as Output in the Console tab as shown below -


Though you have not got any compile time error, but when you have run the program, the above run time error is displayed.

8. Now lets resolve this run time error by modifying the Animal Super Class object creation statement to just defining statement as shown below -


9. View the error displayed in the Manually Downcasting statement as shown below -


As we are using the object in the manual downcasting statement without initializing it, we got this error.

10. Select the 'Initialize variable' option in the above error message to the compiler error as shown below -


11. Observe that the error got resolved and the null reference got assigned to the object as shown below -


Observe by following the above few steps (8-11) we have defined the Animal Super Class object instead of creating it and using the object in the manual downcasting statement.

12. Run the 'ClassOne.java' class file and observe that this time , no run time errors are displayed as shown below -


So the run time error displayed in the step 7 is not displayed after changing the object creating to object defining statement.

13. Till now we have downcasted the super class object manually, but lets findout what happens when you don't manually downcast the assigned super class object as shown below -


14. View the error displayed after removing the manual casting (Cat) as shown below -


15. Select the first option 'Add cast to 'Cat' ' in the error message as shown below -


16. Observe that manual casting (Cat) got added before the assigning super class object 'a' as shown below and the error got resolved as shown below -


Hence Automatic casting is not done when you Downcast. You have to manually cast while Downcasting.







Please comment below to feedback or ask questions.

instanceof operator will be explained in the next post