English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

im confused with instantiable and non-instantiable classes.. please give me some examples so that i can fully understand their differences

2006-10-28 17:07:19 · 4 answers · asked by OhtheHeck 1 in Computers & Internet Programming & Design

4 answers

class are templates right? when you instantiate it, it becomes an object. only CONCRETE classes can be instantiable though.. the perfect example of a non-instantiable class is an abstract class because it's not concrete, here's an example

abstract class Human{
public abstract void walk();
public abstract void run();
pulbic abstract void sleep();
}

a class like this can never be instantiated, only when you have a concrete class will you be able to use it.. a concrete class example:

class Guy extends Human{
public void walk(){
System.out.println("Guy walking");
}
public void run(){
System.out.println("Guy running");
}
pulbic void sleep(){
System.out.println("zzZZzzzz");
}
}

a test sample:
class test{
public static void main(String args[]){
Human h = new Human();// <-- this will be an error
Guy g = new Guy(); // this is legal
g.walk();
g.run();
g.sleep();
}
}

hope it clears your mind

2006-10-28 18:58:19 · answer #1 · answered by KewlyBug 1 · 2 0

KewlyBug provided one kind of example: abstract classes. But you can also have concrete classes that cannot be instantiated. The java.lang.System class is an example. There are no public constructors, so you cannot call System sys = new System(), for example. All of the System methods are static, and since System represents your current system, which there can only be one of at a time, it makes sense that you are not allowed to make multiple instances.

2006-10-30 19:31:07 · answer #2 · answered by vincentgl 5 · 0 0

An object is an instance of a class.

A class is a template.

There are some classes that are part of the standard Java library that are not meant to be an object. So, they make them noninstantiable.

2006-10-28 18:13:58 · answer #3 · answered by timespiral 4 · 0 0

http://www.vivarem.com/files/IN/gn/916/ covers both abstract classes and interfaces in java

2006-10-28 23:24:08 · answer #4 · answered by howsureyouare 3 · 0 0

fedest.com, questions and answers