Abstract Class in Java

jskfan
jskfan used Ask the Experts™
on
Abstract Class in Java

abstract class Animal{
   //abstract method
   public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{

   public void sound(){
      System.out.println("Woof");
   }
   public static void main(String args[]){
      Animal obj = new Dog();
      obj.sound();
   }
}


in the Example above, I thought you cannot create and Object from from an Abstract Class, as it is done here:
Animal obj = new Dog();

**Unless I missunderstood the term:
Abstract classes cannot be instantiated

Can someone Help Explain it ?

Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
I thought you cannot create and Object from from an Abstract Class, as it is done here
No, that's not what's being done there. You're creating concrete type Dog and assigning it to a variable of type Animal

Author

Commented:
-- so how do you instanciate a class ?

--how do you call this syntax below ? I believe  it is creating an object of the class Dog.
 is the Key word "New " which is called Constructor or the "New" + Method Dog()
 Animal obj = new Dog()

Thank you
You can think of abstract classes perhaps a bit like stem cells. They're only meaningful when turned into something with a specific application. So your Dog is still an Animal, but . . . there was no such thing as just an Animal . . . until it became a Dog.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

new Dog() is the constructor.

BTW - please put your code into code tags.

Author

Commented:
OK  , so this one, will instanciate an Objetc:
Animal obj = new Dog()

Open in new window



and t this one, will instanciate a Class

Animal a =new Animal()

Open in new window


But I believe by the rule you cannot instanciate an Abstract Class
Top Expert 2016

Commented:
But I believe by the rule you cannot instanciate an Abstract Class
True
As long as you realise that the instantiation of a class results in an object and not another class then fine. (cf your comment).
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
this one, will instanciate an Objetc:
this one, will instanciate a Class

Just want to clear this up, because I think the point that you were making is that you think that these are two different instanciations.

They are not different! You may see different people use different terms as above, but they are talking about the same thing. In fact there is only ONE type of instantiating happening here, and maybe it is better to phrase it as "instanciating an object of a certain class"

Also, it may help to break the above statements down into the two operations that are happening. In both statements, firstly an object is being instanciated of a class, AND secondly that object is being assigned to a variable.

So, in the first statement, you are instanciating an object of class Dog, and then assigning that object to a variable called "obj" of type Animal.

And in the second, you are instanciating an object of class Animal, and then assigning that object to a variable called "a" of type Animal. (note that this would only work IF you had changed Animal to NOT be abstract, ie. you are correct in saying that you can't instanciate an object of an abstract class)
So, in the first statement, you are instanciating an object of class Dog, and then assigning that object to a variable called "obj" of type Animal.

In fact, obj is of type Dog *and* of type Animal at the same time.
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
@krak, sorry in advanced for this ;-) I know exactly what you mean but I think as the original question asker appears to be less experienced in Java, it is beneficial to clarify this...

In fact, obj is of type Dog *and* of type Animal at the same time.

@jskfan,

Yes, the object that "obj" refers to is of type Dog and Animal (and of type Object too).

But the variable itself, "obj" is only of type Animal.


For example, if we were to write a new method in the class Dog, lets call it "wagTail", then you can't do the following...

obj.wagTail();

It will fail because the compiler can't find a method called wagTail in the Animal class. Yes, you can do a cast to the Dog type (because as krakatoa said, it is still an object of type Dog) but I go into casting just now.

Author

Commented:
Thank you Guys
@mccarl

Well the term “variable” is a term used for semantic purposes only between people. The area of memory occupied by that artefact is an Object, so you can’t say that “obj” is only of type Animal because of it being a variable, as there is no difference between it being a “variable” and being an Object, Animal or Dog - except lexicographically, which is not material electronically.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial