JAVA Beginners part 10

A class cannot be marked private. In case you dont want a class to be sub classed/inherited you can mark it as Final.More on Final later.

Override = The method name , parameters and return type must be same in child and parent class.

Vs

Overload = The method name is same but the parameters can be different for example.

parent
{
Boolean turnon();
Boolean turnoff();
}
child
{
Boolean turnon(int level);
}

above one is overloading not override.

One more Rule.

Access level must be the same or much more easy.

parent
{
public Boolean turnon();
Boolean turnoff();
}
child
{
private Boolean turnon();
}
Is not allowed.

Overloading, this has nothing to do with polymorphism or inheritance.
How is it done?
– change return type with change in parameters
– changing only return type is not allowed
– overloaded method can be more restrictive

over

Test Programs 

test5.jpg

test6.jpg

Considering what has been discussed till now, Let say we have Animal class and we dont want anyone to instantiate this class.
Meaning we dont want an Object to be created for the Animal class. How can we accomplish that?

By Marking the class as Abstract. You can still use the Abstract type as a reference. You can make a class Abstract by adding a keyword.

abstract class Canine extends animal
{
public void roam()
}

Some basic interview program ( i was getting a little bored so trying some fun stuff)

multiple.jpg
abstract class canine extends animal
{

}

public class makecanine
{
canine c;
c = new dog();    { This is allowed, you can always assign a super class reference to sub class }

c = new canine(); { This is not allowed , you cannot instantiate the class that is marked as abstract }

}

There can Abstract class and Abstract methods too. Abstract method will have no body. Look below how it looks like
If there is an abstract method in a class then that class needs to be marked as abstract. An Abstract class can have concrete methods.

abstract class test
{
public abstract void eat();
}
class test2
{
}
public class test3
{
}

 The first concrete class that extends the Abstract Method of a class must implement ( means provide a definition/body) them. An abstract class can extend another abstract class and can have no implementation.

Every class in java extends Class Object. it is the super class of everything.
Implied that we can use methods of Class Object like Arraylist.


Some of the famous methods in the Class Object.

1) equals = used to compare two objects

dog d = new dog();
cat c = new cat();
if( d.equals(c))
{
System.out.println(“true”);
}
else
{
System.out.println(“false”);
}

2) getclass = lets you know which is type of the class is an object

cat c = new cat();
System.out.println(c.getclass());

3) hashcode() = prints the hash code for the object. this is a unique id

cat c = new cat();
System.out.println(c.hashcode());

4) toString() = checkout, not sure of the usage

cat c = new cat();
System.out.println(c.toString());


Is the Class Object Abstract = No. Can we override methods of class Object ? some yes lot of them are marked as final. Most common use of instance of an Object is for thread synchronization.

Interesting question. If it is so good to use the polymorphic types then why dont we just make all our methods take and return object ?
Type Safety is one of Java Greatest feature. That guarantees that we wont ask wrong object to do something you meant to ask other object type.

example asking object Ferrari to sing a song or pigeon to shift gears.

Anyhow the compiler would not allow you to do that. You can call methods on an object for which class it belongs to. Since Java is strongly typed.

The Usual


Let say you have an Arraylist of Dog, which will be
ArrayList<Dog> mydog = new ArrayList<Dog>();// holds dog type objects only

dog adog = new dog(); // a dog

mydog.add(adog); // adding dog to the list

dog bdog = mydog.get(0);// assign the dog from the list to a new dog reference variable.

If in case one declared the ArrayList as type object this is how it would look like.
ArrayList<Object> mycat = new ArrayList<Object>(); // holds any object type

cat acat = new cat();// a cat

mycat.add(acat);//adding cat to the list

cat bcat = mycat.get(0);// This wont compile , because bcat is a cat type reference but the mycat.get(0) will return type object. An Object can go inside an ArrayList as a cat or a dog or a fish or a car but when its coming out ( get method) they will come out as generic instances of object. 

Little more…

public void go()
{
dog adog = new dog();
dog samedog = getobject(adog);
^ above line wont compile, as the getobject method would return an object which is incompatible with the dog type.
}

public object getobject(object a)
{
return o;
}
^ Error incompatible types.

Below modification will work

public void go()
{
dog adog = new dog();
object samedog = getobject(adog);

}


next page 234

More in next part.

References:

Head First Java 2nd Edition

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s