Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java OO Concept

Tags:

java

oop

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");
    }

}

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.

like image 840
newbie Avatar asked Aug 23 '26 00:08

newbie


2 Answers

  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.

like image 89
les2 Avatar answered Aug 25 '26 13:08

les2


check out this ,it might help you,

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

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

like image 26
Annu Avatar answered Aug 25 '26 13:08

Annu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!