File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class definition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class definition" Watch "Class definition" New topic
Author

Class definition

Jo Eagle
Greenhorn

Joined: Feb 14, 2001
Posts: 13
Lets say I have the following code:
public abstract class Abstrat1{
public abstract void AbstractMethod();
public void ConcreteMethod(){
String strName = "Jo Eagle";
}
}
Question 1 - how do I provide a new class definition for a legal class that will inherit from this class? Using the methods - confused on this.
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
public class Myclass extends Abstrat1{
public void AbstractMethod() {
// some code
}
}
Jo Eagle
Greenhorn

Joined: Feb 14, 2001
Posts: 13
How do I use the Concrete class? String Dog = new String;?
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Jo,
Since you've given an implementation (method body) to abstractMethod() in MyClass, you can now say
<pre>code:</pre>

<code><pre>

MyClass mc = new MyClass();
mc.abstractMethod();
</code></Pre>

Art
[This message has been edited by Art Metzer (edited March 30, 2001).]
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
How do I use the Concrete class? String Dog = new String;?

Do you mean "how do I use the Concrete method ConcreteMethod() in Myclass?"
If so, the answer is that since you provided a body for the method in your Abstract class, the method is inherited in any subclass, just as if you had provided the same implementation in your subclass. So, along with the code in the other replies, now you could say:
mc.ConcreteMethod() ;
That would call the ConcreteMethod() of mc (which is an object of type MyClass).
Note, however, that to do anything useful with the String defined in ConcreteMethod(), you need to change the definition as follows (changes are in red):
public String ConcreteMethod()
{
String strName = "Jo Eagle" ;
return strName ;
}

Now, you can actually store the String returned by the method in a new String, like so:
String cmstr = mc.ConcreteMethod() ;
I hope all this helps. Please post any other questions this post may have raised (or in general that you may have).

------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Jo Eagle
Greenhorn

Joined: Feb 14, 2001
Posts: 13
So then would the following be correct?
public abstract class Abstrat1{
public void AbstractMethod();
public void ConcreteMethod(){
String StrName = "Jo Eagle";
}
}
public class MyClass extends Abstrat1{
public void AbstractMethod(){
MyClass mc = new MyClass();
mc.abstractmethod();
}
public String ConcreteMethod(){
String StrName = "Jo Eagle";
return StrName;
}

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Jo,
Abstract classes are usually used to define general behavior that concrete subclasses are supposed to support. For example, if we define an abstract class Animal that has a sound() behavior, you can expect subclasses of Animal to implement that behavior.
<pre>
abstract class Animal {
abstract String sound();
}
</pre>
Doing so allows you to program to the generalized behavior without actually knowing or caring what the exact subclasses are.
<pre>
class Zoo {
void makeSounds(Animal[] wildthings) {
for (int i = 0; i < wildthings.length; i++) {
if (wildthings[i] != null)
System.out.println(wildthings[i].sound());
}
}
}
</pre>
Thus, if you later define subclasses of Animal:
<pre>
class Lion extends Animal {
String sound() {
return "I am King! Roar!";
}
}
class HouseCat extends Animal {
String sound() {
return "Meow";
}
}
</pre>
You can write this kind of code:
<pre>
Animal[] cats = {new HouseCat(), new Lion()};
Zoo myZoo = new Zoo();
myZoo.makeSounds(cats);
</pre>
You can extend this code indefinitely in the future without having to change the code for Zoo.makeSounds(). For example, you can add new classes:
<pre>
class Tiger extends Animal {
String sound() {
return "Catch me by the tail!";
}
}
class Leopard extends Animal {
String sound() {
return "I never change my spots.";
}
}
</pre>
and then extend your original code:
<pre>
Animal[] cats = {new HouseCat(), new Lion(), new Tiger(), new Leopard()};
Zoo myZoo = new Zoo();
myZoo.makeSounds(cats);
</pre>
Zoo.makeSounds() still works even though it wasn't changed.
Hope this helps.
J.Lacar

[This message has been edited by JUNILU LACAR (edited April 01, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class definition