Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to understand need to make Process.java abstract

I was casually walking through source code of Java core classes. I found that Process.java file is an public abstract class. When I went through code No function have definition. Does that mean that it should have been declared an Interface. Is there any deliberate reason behind it. Can some one help me understand need of such design.

Link to code

like image 775
Amandeep Jiddewar Avatar asked Apr 22 '13 15:04

Amandeep Jiddewar


People also ask

Why you couldn't create an instance from an abstract class?

We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.

Is it mandatory to implement abstract methods in Java?

The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class. All the methods in an interface are implicitly abstract unless the interface methods are static or default.

How do you instantiate an abstract class in Java?

Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract .

Is it OK to call abstract method from constructor in Java?

It's a very bad practice to call an abstract method from a constructor. Methods called from constructors should always be private or final, to prevent overriding.


2 Answers

Process is abstract because it is likely to have a different implementation on each operating system. Application developers don't implement this abstract class; it's implemented as part of the Java runtime.

Since all methods are abstract, it could have been declared as an interface, but that would make the class unable to evolve in the future. For example, new methods should not be added to an interface once it's been declared in a public API, because then any existing implementations will be incompatible. In contrast, a new (concrete) method can be easily added to an abstract class.

Instances of Process are created by the runtime, either through the Runtime class methods, or from a ProcessBuilder instance. Applications generally wouldn't use the new operator to create an instance (of the concrete subclass) directly.

like image 125
erickson Avatar answered Oct 19 '22 23:10

erickson


I suspect the critical difference arises from the fact that you can implement as many interfaces as you wish but you can only extend one class, be it abstract or otherwise.

Making Process abstract therefore ensures that if you actually decide to create one from scratch (i.e. not from a system-supplied factory, which is the normal route) you would not be able to put functionality in a parent class of it. How that helps I'm not sure.

Maybe it's a security thing since processes are supposed to be created and owned by the operating system. It is doing it's best to discourage you from making them yourself.

Added

Deep down I think the reason it's not an interface is historical. Remember Process has been in java.lang since the year dot and back then an interface was something you implement, not a definition of a facet of an object's personality. That concept grew up much later.

Notice that Process has been around since JDK 1.0 while many of the more useful interfaces arrived much later, the CharSequence interface for example did not appear until JDK 1.4. I think there was a paradigm shift from objects being primary and interfaces being really useful to interfaces being the be all and end all while objects merely implement.

Process would therefore be one of the old classes that were created at the time when a Process was a real object and probably drove an Audi Quattro, not some vague notional namby-pamby miasmic thing that has some methods in it and looks a bit like this.

like image 26
OldCurmudgeon Avatar answered Oct 19 '22 23:10

OldCurmudgeon