• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Class definition

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Myclass extends Abstrat1{
public void AbstractMethod() {
// some code
}
}
 
Jo Eagle
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I use the Concrete class? String Dog = new String;?
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jo Eagle
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}

 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic