• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm new to JAVA. studying only for last ten days. I've problem in

understanding one program given in the Headstart book in chapter 8.
the program goes like this:
------------------------------------------
interface Nose{
public int iMethod();
}
abstract class Picasso implements Nose{
{
public int iMethod();
return 7;
}
}
class Clowns extends Picasso{}

clas Acts extends Picasso{
public int iMethod(){
return 5;
}
}

public class Of76 extends Clowns{
public static void main(String[] args){
Nose[] i=new Nose[3];
i[0]=new Acts();
i[1]=new Clowns();
i[2]=new Of76();

for(int x=0;x<3;x++){
System.out.println(i[x]. public int iMethod()+" " +i[x].getClass());
}
}
}
--------------------

My question is: Picasso is the superclass of classes Acts,Clowns,Of76.

And since Picasso implements Nose interface, therefore effectively all

the subclasses implement Nose. Is because of this, "i" can be created as

the reference variable for all classes that implement Nose(like Acts,

Clowns, Of76)?the

Nose[] i= new Nose[3]; line is confusing me. what we're doing here

actually creating an array of objects?

Another question is, if the iMethod() contains one System.out...line

,then also do I have to put a return type? can i return String? when?
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Originally posted by Imran Kazi:

Nose[] i= new Nose[3]; line is confusing me. what we're doing here
actually creating an array of objects?


No; you're creating an array of references to objects. Each reference is initially null -- it refers to no object. Then you assign a new object to each of the three references in the next three lines.


Another question is, if the iMethod() contains one System.out...line
,then also do I have to put a return type? can i return String? when?


iMethod() is declared to return int, so all implementations must return int, and not any other type. Printing something has no effect on this at all.
 
Imran Kazi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well well, yes that's reference to objects i stated wrongly..

but my original question was how ew can write nose[] i= new Nose[3]. here Nose is acting as an object?

and about the return type of method, i know return types must be same.. but my question was is it necessary to return something always?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nose[] = new Nose[3];

This does not create an instance of Nose. And you cant create also, since its an interface.It just creates an array of Nose type.

Regarding returning int for iMethod(), it has the same effect of writing SOP in each of the implementations. This way we can avoid writing it for each class and make the caller decide what to do with the returned data.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Imran Kazi:
...how ew can write nose[] i= new Nose[3]. here Nose is acting as an object? ...

Here, Nose declares the type of references the array will hold.
Originally posted by Imran Kazi:
...and about the return type of method ... but my question was is it necessary to return something always?

If a method's return type is void, then no return statement is needed. But if a method declares that it returns something other than void, then a return statement is required.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!