Pezze
Pezze

Reputation: 771

Instance of a class inheritance

I am currently studying java. I have a question about inheritance. I am aware you can inherit the variables of the parent class using:

public class ChildClass extends ParentClass{
}

However, i wanted to do somethin like inheriting the variables and the values assigned to them from an instance of the ParentClass. Example: i have a class Animal and a class Dog extending from Animal. The class animal has:

private int number_of_legs;
private int number_of_eyes;

At this point i create a new instance of the class animal:

Animal first_animal = new Animal(4,2); 
/*where 4 and 2 are the number of legs and eyes which 
 *i attribute through a constructor present in Animal.
 */

Now i want to create a new instance of the class Dog which inherits the values from the instance of the class Animal i just created (first_animal).

Dog first_dog = new ??? // i tried with ...new first_animal.Dog but it didn't work.

Can anyone help me? Thanks for your time.

Upvotes: 1

Views: 808

Answers (7)

JamesA
JamesA

Reputation: 115

I believe that I came here with the same question you were posing. After reading the answers provided, and thinking about it further I realized what I needed to do.

Say you have an Array of Animals, of which one is a Dog. I want to take the attributes from the Animal[0] instance and build a Pet.

You would pass the Animal[0] into the build method.

public Pet (Animal animal,String name){
     this.Legs = animal.Legs;
     this.Tail = animal.Tail;
     this.Species = animal.Species;
     etc....
     this.Name = name
}

So while the Pet object isn't "inheriting" the variables from the Animal instance, it will be taking on its values.

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

This will do:

Dog dog = new Dog(4, 2);

You cannot create an instance of a subclass as an instance of a super class. You can say that all Dogs are Animals, but not that all Animals are Dogs.

Note that the statement above means:

//valid and accepted because one kind of instance of animal is a dog
Animal dog = new Dog(4, 2);
//invalid and throws a compiler error because one kind of instance of a Dog cannot be any animal
Dog anotherDog = new Animal(4, 2);

new Animal.Dog is wrong here because it means that you have a static class Dog inside Animal class. new first_animal.Dog is wrong here because it means that you have an inner class Dog inside Animal class. Those are different topics you should not cover yet.

Upvotes: 1

Dici
Dici

Reputation: 25980

First, you should make your class Animal abstract and set the scope of its attributes to protected instead of private if you need to re-use them in subclasses. To instantiate an Animal, just call the constructor of a concrete subclass of Animal (need to define at least one constructor which calls a superclass constructor as its first statement).

Upvotes: 0

Greycon
Greycon

Reputation: 789

You can't create an instance of Dog which "inherits" from a specific instance of class Animal.

Upvotes: 0

La-comadreja
La-comadreja

Reputation: 5755

Your first problem is that your instance fields are labeled private in your superclass. This restricts their scope to that class, eliminating them from any subclasses. If you would like to expand the scope to subclasses while restricting it otherwise, call the instance fields protected.

Your second problem is that you can't be initializing a new instance of the subclass when the instance already has a class type. What you can do, however, is write:

Animal first_dog = new Dog(4, 2);

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53535

You don't inherit private variables and methods. If you want to inherit them you have to change the access to protected.

A bit about OO and real life: every dog is an animal - but not every animal is a dog, which means that you can do:

Animal animal = new Dog(4,2);

but you can't do vice versa:

Dog dog = new Animal(4,2); // compilation error

Upvotes: 0

Eran
Eran

Reputation: 393906

You pass the values from the constructor of the sub-class to the constructor of the super-class.

Dog firstDog = new Dog (4,2);

The Dog constructor would look like this :

public Dog (int legs, int eyes) 
{
   super (legs,eyes);
}

If you wish to create an instance of a sub-class Dog from an instance of the super-class Animal, you can use a copy constructor :

public Dog (Animal animal) 
{
    super (animal.getLegs(), animal.getEyes());
}

Then you can use it with :

Dog firstDog = new Dog (firstAnimal);

Upvotes: 1

Related Questions