Why "extends" precedes "implements" in class declaration [closed] - java

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Why must implement always be written after extend in a class declaration? For example:
public class Register extends ActionSupport implements ModelDriven
Why can it not be:
public class Register implements ModelDriven extends ActionSupport
The latter produces a compile-time error.

When the Java compiler turns a class into bytecode, it must first look to a parent class. That is because the underlying implementation of classes is to point to the bytecode of the parent class - which holds the relevant methods and fields. Then it adds in pointers to the code of the child class functions - some of which are mandated by the 'implements' keyword.
Because the parent class must be compilable, it is easier if the compiler knows up front what that class is. Further, you can extend only one class but implement any number of interfaces. The compilation time climbs if the extends keyword can be intermingled amongst any number of implements instructions. Compilers want to fail as fast as possible to decrease dev time, so this choice is logical. Further, it helps you think clearly about the class for the same reason.

Probably to make the compiler's job easier. It's just a convention. There isn't any advantage to being able to rearrange these things.
It's like asking why Java functions aren't written in pre-order notation like public int (int a, int b)add{ return a+b; }.

Related

What kind of classes can be exetended by interfaces? What are the rules? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
What kind of classes can be extended by interfaces? What are the rules?
All the Java programmers I know think that interfaces cannot extend classes but
java.util.concurrent.ExecutorService:
public interface ExecutorService extends Executor { //...
java.util.concurrent.Executor:
public class Executors { //...
So, it looks like some interfaces can extend some classes. What are the rules? I only noticed that java.util.concurrent.Executor has static members only and a private constructor. Can somebody explain the rules and the purpose? Thanks.
Interface can only extend another interface and not a class.
Since interfaces have no actual implementation of any logic (this is not possible in Java), there is no fear of possible collisions if some interfaces have the same method signatures.
Executor is an interface and not a class, unless Javadoc is mistaken or I misread your post. So there is not any problem with another interface extend it.
Every single method declared in an interface will have to be implemented in the sub-classes.
A class can implement multiple interfaces and it can not be private.
A class that implements an interface must provide an implementation of all the method of that
interface. as far as doing it the other way around as
#darijan says "Interface can only extend another interface and not a class......."

Does the System.out object belong to class System or class PrintStream? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm new to programming and I just started learning Java. I'm curious that does the object System.out belong to class System or class PrintStream?
I referred to a textbook, JAVA CONCEPTS 4/e. The textbook states that to use the out object in the System class, you must refer it as System.out but later in the book it states that the System.out belongs to class PrintStream.
I've searched google and stackoverflow but all the answers are too hard for me to understand.
"out" belongs to class "System" and is of type "PrintStream" :)
It depends what you mean by "belongs to".
Usually, people would say out "belongs" to System because it is a static field declared in that class. Note, though, that this concept of belonging is only a weak one, basically implying only a namespace ownership. There is no special relation between a class and its static fields.
You may also say the object referred to by the out variable belongs to the PrintStream class because it is an instance of that class (or a subclass), just as "beagle" belongs to the "dog" class. This is not standard usage in Java parlance, but it makes perfect sense from the perspective of type theory, where a type is really a set of values, and the value of out "belongs" to that the type / set defined by the PrintStream class.
System.out is a PrintStream object. It is defined in System (so, it is member of System) class as :
static PrintStream out
See: http://docs.oracle.com/javase/6/docs/api/java/lang/System.html
It is defined as:
static PrintStream out
out is a static field in System. Its type is PrintStream.
In loose simple terms, 'out' is a field (i.e. an object) in class 'System' of type 'PrintStream'.
Out is the static reference variable in class System and is of PrintStream(class) type.

Class, subclass, abstract class, interface [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How do you know whether to use/make a
class, a subclass, an abstract class, or
an interface?
I mean... a class like Math for example it´s not instantiated.
It´s a final class. There´s no IS-A relationship.
That is the purpose of making a class, a class that has no subclasses?
A subclass seems to be/have more specific code of its superclass.
An abstract class is something more generic, it can have implementations for its subclasses.
An interface is used/implemented in abstract classes and subclasses when those need to play a role.
Am I right?
Also..
A superclass is an abstract class?
In general
interface - to group a related behaviors that can be added to an object.
abstract class - when you need to create a base class with common states and behaviors that should not be instantiated
class - to come up with an implementation
subclasses - when you need some additional behaviors in an extensible class
Try to make a logical tree structure of classes. Then classes will be everything that is supposed to be directly used by other code, and subclasses will be more specific versions of parent-classes. Abstract classes will be parent-classes that isn't specific enough to be used in any way and interfaces will be abstractions of a type of classes, like a class but with no implementation at all. More or less...
That is the fast explanation. Clearly by asking a question like this you do not know the fundamentals of OOP so you should find a book to read about it, because nobody could explain all the related concepts in one post like this.
Class: Every piece of code you write in java, is with in a class, so you consider class as a Data type, which describes what an object is and what it does.. (ie Instance variables and methods.)
Sub-Class: Usually needed when we want all the functionality of a certain class in our own class.
Abstact-Class and Interface: Now the reason why i tugged both of these together here, is because in Design Pattern Abstract class or an Interface is need to showcase a behaviour which keeps changing...

is Polymorphism also applies in Abstract Classes? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
A common argument is that Polymorphism only applies to interfaces and not abstract classes.
Is the relationship of as Abstract parent class to a concrete class that extends it considered a polymorphism?
List list = new Arraylist()// Polymorphism as List is an interface
AbstractClass parent = new Child(); // Is this also considered polymorphic?
Common argument is Polymorphism only applies to Code to interface and Not abstract classes.
That "argument" is factually incorrect.
Polymorphism works just fine in Java whether you are using interfaces, abstract classes or non-abstract classes. (There are questions about which is best for long term maintainability, but that is a different topic.)
I'd be curious where you found that "argument". Can you provide a URL?
Yes. That is polymorphic.......

Can someone explain what is a HelperClass and what does in Java or in C++? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want to know what the HelperClass used for. Can someone please explain it with examples?
Thank You
A helper class (in Java, at least) is usually just a static utility class with a variety of methods. In general, a single helper class deals with related functionality, like Apache Commons' StringUtils class, that has a bunch if static methods that operate on strings.
In Java, these types of classes often exist to wrap up functionality that either cannot (for example, the String class is final, so additional functionality can't be added) or should not be addedto the class itself.
In a nutshell, however, a helper class is a class that helps, by providing general or specific functionality in a generic, reusable, encapsulated way.
It's usually used to preserve the cohesion of a class (in Java). Useful methods that don't really belong in any of the classes context can be put in a helper class (static of course) so you can call them.
They are usually static methods. They dont belong to a class or a context of the class, but instead using a class, you can provide some sort of functionality with a helper class/ static classes.
Informal way of referring to Utility Class

Categories