开发者

Java OO Concept

Good day!

I am reviewing the Java OO concept. And wrote the following codes:

  public class Main {  
        public static void main(String[] args) {
            Animal dog = new Dog();
            dog.eat();
            dog.sleep();
        }

    }

abstract public class Animal {

    private int age;
    public Animal (){
        age = 1;
    } 
    public Animal (int age){   //How can I call this constructor?
        this.age = age;
    }
    public void eat(){
        System.out.println("Eat");
    }
    abstract public void sleep();
}

abstract public class Canine extends Animal{

    abstract public void roam();

}

public interface Pet {

    public String petName = null;  //i want the pets to have a variable petName.
    public void trick();
    }


public class Dog extends Canine implements Pet{

    public void roam(){
        System.out.println("roam");
    };

    public void sleep(){
        System.out.println("sleep");
    };

    public void eat(){
        System.out.println("Eat Dog");
    };

    public void trick(){
        System.out.println("trick");
    }

}开发者_StackOverflow

I have several questions as follows:

  1. How can I call the Animal Overloaded constructor?

  2. How can I use the variable petName in the PET Interface?

  3. Am I doing the concept of OO correctly? What rules am I violating?

Thank you in advance.


  1. Subclasses will call the super constructor from within their own constructor using super(...) as the first line!
  2. Interfaces cannot have variables or state - only methods!
  3. You have a sound concept, but your code would not compile (because of item 2 above).

Some solutions:

public interface Pet {
    String getName();
    void trick();
}

Now the Dog class (or any class that implements Pet) will have to implement Pet.getName(). Give the Dog class a field of type String called 'name' and return it from Dog.getName().

public abstract class Canine extends Animal {
    public Canine(int age) {
        super(age); // pass the age parameter up to Animal
    }
    ...
}

public class Dog extends Canine implements Pet {
    private final String name;
    public Dog(String name,int age) {
       super(age); // call the age constructor
       this.name=name;
    }
    public String getName() { return name; }
    ... rest of class ...
}

Each subclass (esp. the abstract ones) will need to provide matching constructors for all parent class constructors you want to call! (So I added the age parameter to the Canine constructor so that Dog could pass an age argument to it.


check out this ,it might help you,

http://www.roseindia.net/java/beginners/constructoroverloading.shtml

http://www.jchq.net/certkey/0602certkey.htm


  1. Calling either constructor is calling an overloaded constructor: both constructors use the same name. To call the constructor that takes an int, call dog = new Dog(1);.

  2. The class Dog implements Pet, so it will have a public field petName (is it super.petName?).

  3. I don't see any fundamental errors.


1) you can call argumented contructor as" this(20);"

Example of explicit this constructor call

public class Point {

int mx;

int my;

//============ Constructor
public Point(int x, int y) {
    mx = x;
    my = y;
}

//============ Parameterless default constructor
public Point() {
    this(0, 0);  // Calls other constructor.
}
. . .

}super(...) - The superclass (parent) constructor An object has the fields of its own class plus all fields of its parent class, grandparent class, all the way up to the root class Object. It's necessary to initialize all fields, therefore all constructors must be called! The Java compiler automatically inserts the necessary constructor calls in the process of constructor chaining, or you can do it explicitly.

The Java compiler inserts a call to the parent constructor (super) if you don't have a constructor call as the first statement of you constructor. The following is the equivalent of the constuctor above.

//============ Constructor (same as in above example)
public Point(int x, int y) {
    super();  // Automatically done if you don't call constructor here.
    m_x = x;
    m_y = y;
}Why you might want to call super explicitly

Normally, you won't need to call the constructor for your parent class because it's automatically generated, but there are two cases where this is necessary.

You want to call a parent constructor which has parameters (the automatically generated super constructor call has no parameters). There is no parameterless parent constructor because only constructors with parameters are defined in the parent class.


  1. A call to the parent constructor is super(); in your case it would be super(12); for example. Note that a call to a constructor (either this() or super() must be the first statement. Note that you can only go up one level, so to call the constructor in Animal, you'd have to code that into Canine and not Dog.

    public Dog(int age)
    {
         super(age); //this will invoke Canine's overloaded constructor
    }
    
    
    //You must now provide the overloaded constructor in Canine which invokes
    //the overloaded constructor in Animal
    public Canine(int age)
    {
         super(age); //this will invoke Animal's overloaded constructor
    }
    
  2. You may access the value of Pet.petName as, well, Pet.petName. This is because Interfaces can only have methods (which are implicitly public) and constants (which are implicitly public static final). You could use this.petName but it's pointless (and potentially confusing). You cannot set the value of petName anywhere else, ever since it's final, even if you don't declare it to be.

    However in this case, declaring the name as part of the interface doesn't make sense since it's not constant. It should be a part of some abstract or concrete class so that it can be set per instance of Pet. You should instead define a getter (and maybe a setter) for Pet -

    public String getName();
    public String setName(String name);
    

    these will force implementing classes to provide some sort of implementation to get and set the name for a Pet without enforcing how that name is stored inside of that class.


How can I call the Animal Overloaded constructor?

You need to chain it.

abstract public class Canine extends Animal{
    public Canine(int age) {
            super(age);
    }
    // ...
}

and

public class Dog extends Canine implements Pet{

public Dog(int age) {
            super(age);
    }
   //...
}


How can I use the variable petName in the PET Interface?


Pet.petName. If a class implements such an interface, then the class can refer to those constants without a qualifying class name like this: petName

Am I doing the concept of OO correctly? What rules am I violating?

Interface constants

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜