JAVA Beginners part 11

The compiler decides whether you can call a method based on the reference type, not the actual object type. Any object that you create is more than a simple Object. It has access to all of the methods of the Class Object.


All these stories are useful for this concept. If there is a Dog object is made as a generic object, you can get the Dog object out of that by type casting. Key here is You must be sure the object is really a Dog. 

Object o = al.get(index);
Dog d = (Dog) o; // Type casting 

wait a minute , if your are not sure if that is dog or not, you can use instanceof operator to check. Because if you are wrong then you will get a classcastexception at run time.

if ( o instanceof Dog)
Dog d = (Dog) o;

Iterating again, JVM at run time will check the class of the Reference variable not the class of actual object .

There is problem in the textbook, In a Scenario of Animal -> Dog,Tiger,Hippo,Cat Hierarchy if we need a pet animal behaviors then it is best to create a new class called Pet and have all those behaviors saved in the new class and then make the Cat and Dog extend the new class Pet.

But there is a one problem with that approach, this will lead to multiple inheritance. And Multiple inheritance is not allowed in JAVA.

This is how a new concept call Interface is introduced. 

Problem of multiple inheritance is solve using Interface. Interface is 100% Abstract class. All the methods in an Interface is abstract. Any subclass that is implementing this must have the implementation. So at the run time JVM will not be confused, since the subclass has to have the implementation there is no need to double check which one to use.  

public interface pet


public class dog extends canine implements pet

Interface methods are public and abstract, so it is not necessary to specify them using keywords

public interface pet
public abstract void befriendly();
public abstract void play();
public class dog extends canine implements pet
public void befriendly()

}// implement the methods of pet interface
Interfaces do not have any code which is a good, that way the class that is implementing them can define their own methods which is often the case. Interface will define the method name , the signature and return type that will be common for all the classes that implements them. A class can be in any inheritance tree and it can still
implement any Interface that is the power of the Interface.
Objects must be from a class that is a subclass of the polymorphic type.
But with interface as polymorphic type the objects can be from anywhere in the inheritance tree.
some uses of Interface
– Object to save its state to a file, implement Serializable Interface
– Run the methods in a separate thread of execution, implement Runnable interface

you can extend only one class but implement many interfaces.
Now then here is what you need to know.

Class – it does not pass a Is-A test. or any other type
Sub Class – specific version of a Class. Follows the Is-A test
Abstract Class – When you want to create a template for the sub class and may be have few concrete methods.
Interface – When you want o define a role other classes can play

Remember we talked about Super keyword sometime back. Super is used to invoke the
super class methods from within a subclass.

abstract class report
void run_report()
//generic stuff
class buzzwordsreport extends report
void run_report()
super.run_report();// calls the super class version to do some generic stuff

…specific stuff


Reference variable of type object and be used to call methods defined in class object, regardless of the type of the object to which the reference refers.

ArrayList<dog> is a way to tell compiler that you want only dogs to be put in that list nothing else. ArrayList actually gives only objects. Since the compiler does the typecasting for us at run time, we get the dog out of the arraylist instead of a generic object.




next page 257

More in next part.


Head First Java 2nd Edition


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s