JAVA Beginners part 9

Code Magnet



It is a way to reuse a code and make go away the redundant ones. This is one of the pillars of Object Oriented programming. Stand out feature of Java.

Much easier to learn through example.

Let say, there are three shapes. Triangle, Rectangle. You want all to calculate area of the shapes. Instead of writing a 3 separate method for each, i can write one common method and have all the common features which all the shapes can have in a common class and then make all the subclass inherit those functionalities from the parent( Just like parent child heredity in biology)


Read it as, Circle Inherits from Shape. Shape is super class and Circle is Subclass. Subclass gets all functionality of Super class Automatically.  

Concepts of Inheritance

Override. If a subclass does not want to inherit the super class method then it can redefine the method and override the method of super class. JVM will take care of this at runtime.


Which Method will be called ?


In the above case, the lower method will be called. Which means the overridden method in wolf class will be called for makenoise() and eat() You can also call the parent class but that can be done using special keywords like Super. For now lets stick to the above.

To check if you have defined the classes and sub classes properly , try the below test.
It is called Is a test.
Triangle extends shape so Triangle is a shape
Tiger extends Animal,so Tiger is an Animal

In the similar lines , you can say Animal has a Tiger and Shape has a Triangle

Sub classes need not have to be recompiled if any change has been made to the Super Class. The Super class can be recompiled alone and the changes will reflect in sub class.

Euler Problem 4 ( Fooling around)



Normally, When we say

dog mydog = new dog();

Here mydog is the reference variable, new keyword creates an object in heap and assigns that to the variable mydog. The Reference variable and object created are of same type which is dog.

With this new concept called polymorphism we cant created diverse types for instance

reference variable and the object type can be different.

animal mydog = new dog();

Let see how does Polymorphism works.

Any object that passes the Is a relationship can be assigned to a reference variable or Anything that extends the reference variable type can be assigned the reference variable. 


Create a Array of type animals.
Animal[] animals = new Animal[5];

animals[0] = new dog();
animals[1] = new cat();
animals[2] = new wolf();

In the above assignment dog class extends animal. animal is a super class and dog is a subclass.
Trick is animals variable can hold any number and type of animals like dog or a cat or a wolf.

you can even loop through each element and call the animal class method on each object.
When the i=0 eat method of Dog will be called and when i=1 eat method of cat will be called and so on.

for(int i=0;i<animals.length;i++)

Another utility of Super class Reference,

pass it as a parameter to a method and that method can take multiple type of objects as inputs.

class vet
public void giveshot(animal a)

class petowner
public void start()
vet v = new vet();
dog d = new dog();
hippo h = new hippo();


next page 210

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