I am a java beginner trying to learn some java.
Can any one give me overview of difference between Static and Abstract in Java with some example?
I am a java beginner trying to learn some java.
Can any one give me overview of difference between Static and Abstract in Java with some example?
There may be one difference is that we can not create an instance of abstract class but we can create an instance of static class.
for example
if have static class
static class ABC{
void show(){
System.out.println("hello world");
}
}
//we can define function body in static class to call show function we can create an instance of static class like
ABC obj=new ABC();
obj.show();
it give out put
hello world
but in abstract class
abstract class ABC{
abstract public void show();
}
// we can only declare abstract function in abstract class to call that function we have to create another class and extends abstract class
like
class Another extends ABC{
// now define function body of abstract functon
public void show(){
System.out.println("hello world");
}
}
now to print output we can create an instance of Another class
Another obj=new Another();
obj.show();
it will give you the output
hello world
i think this type of explanation you want
static and `abstract` are two keyword in java,
it's depend where it's actually written i.e. before class/variable/method..etc.
Basically,
static
can be written before classes+instance-variables+instance-methods
, after written it won't become instance now it's change from instance to class-level.
abstract
can be written in front of class and methods only.
Let's say, static
first then,
if you write static
class (if class is inner) then it will become a member of outer class and behavior same as other static member of class
.
if you write static
in front of instance-variable or instance-method
then it's single copy maintain through out as many object of that class.
also, take care about while inheritance
in case of.
if class
is abstract
then you can not instantiate
any object of that class whereas you must have to gone through inheritance
.
if any method is abstract
then you must have to override it, also be aware then if any single method is abstract
then you must have to declare that class as an abstract
.
So, i have just given overview of it. in detail all cover here is not possible.
Let me know if any serious matter i forget then.