• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract class  RSS feed

 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to do an extra class in my project and it should be en abstract class. Why? Because I should do (make?) something with abstract...(or interface).

Thanks

[ May 11, 2008: Message edited by: Leni Kravitskki ]
[ May 13, 2008: Message edited by: Leni Kravitskki ]
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leni Kravitskki:
I have a class membership (superclass), 2 subclasses and a SwimClub class.
Can somebody suggests what I can do?
Is this possible to make an abstract class from SwimClub class if I make an extra class SwimClub2? Abstract class SportClub?
The membership class which has two subclasses might be abstract, and a SportClub would be a potential abstract class too. You will have to check how you are using the classes. It would have been a lot easier to design an abstract class or an interface at the beginning, than trying to insert it late in your project.
 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ;-)

What is wrong with my abstract class?

public abstract class Member
{
private int ID;

public void setID(int ID)
{
this.ID = ID;
}

public int getID()
{
return ID;
}
}
---------------------------------------------

public class You1 extends Member
{
private int age;

/**
* Constructor
*/
public You1(int age)
{
this.age = age;
}

/**
*
*
*/
public int getAge()
{
return age;
}

public void print()
{
System.out.println("Age:" + age);
}
}
[ May 13, 2008: Message edited by: Leni Kravitskki ]
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leni,

Why do you think something's wrong with your abstract class?

It's not a particularly good example of using an abstract class, since the subclasses don't use getID at all, so there's no sense of why you're extending these classes from that abstract class.

Also, please post your code in the UBB code tags to make it more readable.
[ May 11, 2008: Message edited by: Stevi Deter ]
 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leni,

Why do you think something's wrong with your abstract class?

It's not a particularly good example of using an abstract class, since the subclasses don't use getID at all, so there's no sense of why you're extending these classes from that abstract class.

Also, please post your code in the UBB code tags to make it more readable.
[ May 11, 2008: Message edited by: Stevi Deter ]


1) What is that? UBB code? Thanks.

2) What can I do to make an abstract class with those 2 subclasses?
Please, can you show me an example in my abstract class Member...
I have understod that abstract classes use something with "verbs".
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leni,
In the editing screen when you are writing your post, look below the "Add Reply" button. You'll see "Instance UBB Code" and some buttons (URL, EMAIL, BOLD...etc) that let you generate UBB Code, which is what this forum uses instead of HTML. One of those buttons is "Code". Click that and then paste your code between the open and close tags to keep formatting.

It's very important to understand abstract classes, their uses, and how they differ from interfaces if you're going to succeed in Java (and object oriented design and programming in general).

Try reviewing the Java Tutorial on Abstract Classes and the JavaRanch FAQ on Interface vs Abstract classes to understand the purpose of an Abstract Class, and that should help you decide what code to put in your abstract class and what code to put in your subclasses.
[ May 11, 2008: Message edited by: Stevi Deter ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leni Kravitskki:
(...snip...)2) What can I do to make an abstract class with those 2 subclasses?(...snip...)


Right below the area where you type your response are the words:Instant UBB Code and below that are ten buttons labeled: URL / EMAIL /BOLD /ITALICS /QUOTE / CODE / ...

 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

I have tried to make a abstract class for days...but still nothing.


Have 2 subclasses and 1 abstract class.
Can I add public String WantToBeAGoodSwimmer or something else in subclasses and move this in my abstract class?
How?





[ May 13, 2008: Message edited by: Leni Kravitskki ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write in a class that extends an abstract class a method with exactly the same name and signature - which means the same method name with spelling and capitalization exactly the same with the types between ( and ) in the same order and exactly the same type, then in that class the methods operate in an instantiation of that class as described in that class. Thus Swimmer and CompetitionSwimmer, if they both share variables can have the variables written in the base class ( may be an abstract class ) and each will use the variables as described in that class if you write methods in Swimmer and CompetitionSwimmer that are specific to that type of swimmer.

A base class does not have to be abstract, that is programmer notation such that one does not declare a member without specifiying how a type of member does things.

I have cut back on the amount of code because it sounds like this is a homework assignment. We don't do homework, I am only trying to get you unstuck.


Your constructors are showing progress, examine the above advice to make them work better. Also, compiler errors are faster than reading books. Go to the books to figure out a compiler error.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!