Solved

subclass

Posted on 2004-09-12
1
242 Views
Last Modified: 2010-03-31
Hi Experts,

    I already declared name and age in the superclass, why that I can't use them in the subclass line 01 & line 02 ?

   Thanks !!!

--------------------
public abstract class MyAnimal {
   
    /** Creates a new instance of MyAnimal */
    public MyAnimal() {
    }
    String name ;
    int age ;
    void Talk(){System.out.println("Each animal has its own language."); }
    void Eat(){System.out.println("Each animal needs food.") ;}
    void Weight(){System.out.println("I don't like fat animals !") ;}
}
----------------------------------------
public class MyDog extends MyAnimal{
   
    name = new String("Doggy") ;            // line 01
    age = new int(5) ;                              // line 02
    /** Creates a new instance of MyDog */
    public MyDog() {
        System.out.println("I am a dog. My name is Doggy !") ;
    }
   
    void Talk(){
        System.out.println("    Woof ! I talk as Woof Woof !") ;
    }
   
    void Eat(){
        System.out.println("    Woof ! I like to eat bones.") ;
    }
   
    void Weight(){
        System.out.println("    Woof ! I weight 60 pounds.") ;
    }
}
0
Comment
Question by:meow00
1 Comment
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 12040749
because its not in a method

try:

    public MyDog() {
       name = "Doggy" ;  
       age = 5;    
        System.out.println("I am a dog. My name is Doggy !") ;
    }
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 46
Oracle SQL syntax check  without executing 6 74
java stored proc example 9 44
SequenceInputStream example 3 19
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question