开发者

When Abstract Classess are to be used actually? [duplicate]

开发者 https://www.devze.com 2023-01-06 16:33 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Why abstract classes necessary?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Why abstract classes necessary?

here is one program which has 1开发者_开发知识库 abstract class without abstract method. its also works fine. Then what the use of abstract class, any how it does not allow to create objects, we can use this with concrete class to implement method defined in abstract class which we have to extend the abstract class then why not directly in concrete class why need of abstract class. can anyone please explain this?

PROGRAM:

abstract class demo {
public void show() {
System.out.println("not abstract method");
}
// public abstract void display();
}
class demo1 extends demo {
public void display() {
System.out.println("abstract method");
}}

class program {
public static void main(String args[]) {
demo d = new demo1();
d.show();
d.display();
//d.display1();
}
}


Sometimes it is simply useful to know that a object was created from a class that was extended from some "parent" abstract class. For example, if you're writing a custom event handler with many different kinds of events, you wont know what sort of data each event will contain or what methods are needed to use that data. If you created a parent abstract class called "Event" you could then extend that class into MouseEvent, KeybaordEvent, NetworkEvent, etc and pass them all to an event handler method such as

public void handleEvent( Event e ){ ... }

Then you can determine what specific sort of event object it is and handle it accordingly.

0

精彩评论

暂无评论...
验证码 换一张
取 消