?
Solved

subclass

Posted on 2004-09-12
1
Medium Priority
?
267 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

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