• Post Reply Bookmark Topic Watch Topic
  • New Topic

will this class compile?  RSS feed

 
raavi kumaar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Will this class compile?

public class Airplane {

public void takeoff (){}

public abstract void fly ();

public abstract void land();
}

If Yes, state the reason.If No, please explain.

[EJFH: Invitation to take this offline removed. ]

[ October 29, 2007: Message edited by: Ernest Friedman-Hill ]

Now, the answer and explanation for the question I posed.

As Abhishek has replied -even if one of the methods is marked as abstract, the entire class has to be declared abstract is absolutely right.
Also, he has gone ahead and suggested the solution too! Kudos buddy, you made my job easy :-)

Please find the solution below:

Solution #1

public abstract class Airplane {
public void takeoff (){}

public abstract void fly ();

public abstract void land();
}

Solution #2

public class Airplane {
public void takeoff (){}

public void fly () {}

public void land(){}
}

Eureka !!!

[ October 30, 2007: Message edited by: raavi kumaar ]

[ October 30, 2007: Message edited by: raavi kumaar ]

[ October 30, 2007: Message edited by: raavi kumaar ]
[ October 30, 2007: Message edited by: raavi kumaar ]
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no the class wont complie because if you have even a single abstract method in your class you have to declare the class as abstract
so to complie your class either mark the class abstract or make the methods non-abstract by providing a body
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the original poster: -

You're new to JavaRanch, so please read the JavaRanchFaq. In particular, DoYourOwnHomework and UseTheForumNotEmail.


To the person who answered: -

Please read LetThemDoTheirOwnHomework.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you need to ask yourself: will this code compile, the best answer is always: just try it and see what happens.
 
raavi kumaar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
To the original poster: -

You're new to JavaRanch, so please read the JavaRanchFaq. In particular, DoYourOwnHomework and UseTheForumNotEmail.


To the person who answered: -

Please read LetThemDoTheirOwnHomework.



Yes Abhishek, You are absolutely right as always!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!