File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Creating Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Creating Objects" Watch "Creating Objects" New topic

Creating Objects

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I have a Fruit class

This is out of the SCJP book by Kathy and Bert. I added some code to play with. The line Fruit b = new Apple(); what does this actually do?
If I do Fruit b = new Fruit I get an error because Fruit is abstract I know that.
[ July 26, 2007: Message edited by: James Hambrick ]

Visit my blog!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Fruit b = new Apple() creates an Apple object but creates a reference that refers to it as a Fruit.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

huh, doesn't make sense why that would be useful, except for maybe that confusing Composition that I read about in Thinking in Java 3.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
It could make sense.

If you have generic code in which you only need the properties of Fruit and want to use this method for Apples and Peers which extends fruit.

Off course a better way to achieve this is the use of interfaces which offer more flexibility.

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

sorry, but thats in the next chapter. I will hopefully understand this and find my answer in the next chapter. I was taught at college procedural programming with COBOL, C and VB6. Never really used objects much. I graduated in 2002 so they should have showed me some object stuff, more than they did. My C++ professor did not know C, my web programming professor taught us HTML and when we got to Java he just expected us to edit a program to add functinality. at this time we only had taken COBOL, one student took C by then and kinda knew what he was doing.
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
Remko -

Why do say that an interface would give greater flexibility.

I know that you could descend from another class and implement the interface Fruit. But other than this what other benefit making it an interface confers ?

Dodo Anakin

Joined: Jul 19, 2007
Posts: 10
hehe , so funny !!
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Here we are doing.
Program to a supertype. Here super type can be interface or a abstract class. The point here is to exploit polymorphism by programming to a supertype so that the actual runtime object isnt locked in the code. With the power polymorphism we can change the behavior of objects at run time.
Here is a simple example.

Now suppose my requirement is to write a method that takes a list of any animal object and to make them make sound , If I would program to a super type , I will have to do the following

So here in Test class i will have to write three methods for each Animal object so that i could make them make Sound. Now in future if i have another new animal i will have to modify Test class and include another new method for the new animal , And this will go on for ever.

Now comes Program to supertye

Now i have only one method that takes Animal reference as arugument. Since i have programmed to super type Animal a = dog , All i have to do is just invoke the method makeSound() with Animal reference, Now @ run time depending on the appropriate object type, makeSound() will be invoked. With this approach if in future i have a new animal type, I will not have to modify my Test class. Hence the class is closed for modification.
Always we need to code in such a way that A class is closed for modification. .
I can go on and on on this topic. But i hope, Now you understand the power of programming to supertype.

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy ranchers!

James wrote:
sorry, but thats (generics)in the next chapter.

You're right cowboy. Don't try to ride a wild mustang when you have problems with ponys.
What we are talking about here is called polymorphy and this is one of the key concepts of object oriented programming.

I modified Deepak's code a bit so it is now compiling:

In class Zoo the variable "animal" is of type Animal and can take any subtype of Animal as an object. Only not Animal itself because the class is abstract and cannot be instanciated. Hence no animal objects, only Animal type.
But in this code only at runtime (due to the random) it is clear, what kind of an animal the variable will hold.
The same is true for the array named "zoo". In the line "assing" an object of a subclass of Animal (thats is refered by the variable "animal") will taken as an element of the array.

And in the listening part you will also see polymorphy. Class animal demands a method makeSound from all non-abstract classes that extend Animal. It also has a non-abstract method (toString) that will be inherited by the subclasses.

Output is something like:
I am a Duck Quack
I am a Dog Bark
I am a Dog Bark
I am a Dog Bark
I am a Dog Bark
I am a Dog Bark
I am a Duck Quack
I am a Dog Bark
I am a Duck Quack
I am a Cat Meow

So having something like
Animal any = new Cat();
It is sure, that the object refered by variable any surely has all methods, that are abstract in Animal in a concrete implementation.

An interface can be regarded as a special form of an abstract class that has no concrete (non-abstract) methods at all. But that would be a differend thread.

See also: How my dog learned polymorphism.

"anakindodoloveme anakindodoloveme" posted:
hehe , so funny !!

The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:

So, "anakindodoloveme anakindodoloveme" could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


all events occur in real time
I agree. Here's the link:
subject: Creating Objects
It's not a secret anymore!