• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating Objects

 
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fruit b = new Apple() creates an Apple object but creates a reference that refers to it as a Fruit.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

Thanks
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hehe , so funny !!
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Thanks
Deepak
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
http://www.javaranch.com/name.jsp


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.



Regards,
Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic