Why do many Java API methods have “abstract” modifiers?

Student level question, I hope stackOverflow can help me get a deeper understanding here:

I noticed that many of the methods (for example, in Graphics2D , for example) in the Java API have “abstract” modifiers. Why is this?

I'm just wrapping my head around inheritance and polymorphism right now, but as I understand it, an abstract class is intended to be used as a kind of “parent class” that is extended by subclasses, and this abstract classes themselves cannot be created.

I also understand that abstract methods are declared (but not implemented) in an abstract class and must presumably be implemented in a subclass in order for this subclass to be considered non-abstract (and therefore be real).

So, how can I import a class like Graphics2D, instantiate it, and use its abstract methods?

My instructor mentioned that methods are overwritten at runtime (?), But I don't understand how / why. Obviously, I missed something here, I would like to know more!

+4
source share
2 answers

So, how can I import a class like Graphics2D, instantiate it, and use its abstract methods?

Because you never instantiate Graphics2Ddirectly. Look at your code - you will not find this:

Graphics2D graphics = new Graphics2D(); // This wouldn't compile

... . , Graphics2D, . , , , Graphics Graphics2D, ... , .

, , , , ... Graphics2D, ..

, , - , , , .

+8

; C/++.

, Java, InputStream. , , , .

, InputStream, , , . .

, InputStream, GZIPInputStream FileInputStream. GZIPInputStream . FileInputStream , . , .

+1

Source: https://habr.com/ru/post/1611866/


All Articles