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...
Related
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......."
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 understand that Java Interfaces are used for declaring methods that will be used in more classes as those classes implement them. For example:
public interface MyInterface{
void myVoidMethod();
Bool myBoolMethod(int x);
String myStringMethod(String a,String b);
}
So, from what I understand, it is mainly used just to make sure some methods are declared in a class and those methods are usually ones that are more general, and might be applied in more classes. But what about methods that should already contain some code and should apply to more classes?
For example what if I have many classes where I need myBoolMethod defined as
Bool myBoolMethod(int x){
return x?0:1;}
(I gave just a simple example, but obviously I would need to use more complex methods). I've heard about "code injection" and I'm pretty sure it's related to it, but I have no clue how to go further, neither do I know what to do not to rewrite the same code in different places.
Not necessarily. It is also used for testing. If you don't have an interface you can't provide a custom implementation for your testing purposes. Another concern might be the "code to interfaces not implementations" paradigm. Let me illustrate:
Suppose you have an ArrayList of Strings:
ArrayList<String> list = new ArrayList<String>();
You use this in your code but later you later find out that you'll need a LinkedList because it is more efficient for your problem.
What happens now? You have to refactor your code in many places.
However if you've used the List interface in the first place you could've passed it around without caring about the implementation of it so if you later find out that you need a LinkedList instead of an ArrayList you just have to modify in a single place:
List<String> list = new LinkedList<String>();
If you need cross-class methods you can create an abstract class and provide a default implementation for a method defined in your interface. It will be shared in all your implementing classes in case you don't override it.
With java 7, you would have to use an abstract class to provide a default implementation for a method. There is no way to define some behaviours of your object in an interface.
With java 8 you would be able to provide a default implementation for a method in an interface.
You are looking for abstract classes and inheritance.
More info on inheritance and using the super keyword here:
http://docs.oracle.com/javase/tutorial/java/IandI/super.html
If you wish to ensure that classes have certain methods that already have implementations, then you want to be using an Abstract Class.
An Interface is simply a contract, that stipulates when a class agrees to implement that interface, it MUST provide an implementation for those methods. This is useful when you want to encourage loose coupling in your code, by having objects refer to your class via an interface, rather than via the object's direct methods.
An interface is useful (speaking from practical experience) where you want to specify that a certain variable/parameter should conform to the contract some other posters mention, and nothing extra. E.g. you have a resource that gets injected (say a data source) but you don't want to specify yet whether it should be a connection to a say MySQL DB, a SQL Server DB, or a dummy data source that you are going to use just for testing without affecting live data. So you specify the type as the interface type, and at runtime an instance of the appropriate implementing type gets injected. You could google for some injection (CDI) tutorials if you are interested.
In other cases you would rather want to use a superclass (abstract or not). The correct answer is: it depends, and one would do good to read (and then review) theory and books on Java language fundamentals to be able to make the best choice for your particular circumstances.
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.
When we can have class and method as static?
Anybody please help me with some example...
When you don't need a class instance to invoke that method. It means that method does not depend on any non-static members of the class
You can make a method static if it doesn't use any non-static members of the class.
You can make a class static if it only contains static members.
If a method doesn't change it's behaviour based on different objects of it's enclosing class. it can be marked static.
Check Math class. all of it's methods are static cause, their behaviour just depends on the argument within methods and those methods don't make any change in the class states.
All the utility/helper methods can be (should be) marked as static. i.e. if their behaviour is same for all objects, why to have different copy for each object, just have one copy and let all objects share same copy.
You should check this also: Why are you not able to declare a class as static in Java?
A static method belongs to the Class and thus not to a specific instance. If you think at programming languages in term of message dispatching we can say procedural programming provides only 1 level of message dispatching as the name of the function correspond to its actual behavior. In Object Oriented Programming we have two level of message dispatching as you also has you have to specify an object plus the signature of a function (method). The same function may behave differently on different object depending on their status (subClass overridden method, etc.). A static method is instead like a global function you can execute where and how you want and always has the same behavior.
You may therefore limit the usage of static methods although there are cases in which they are helpful. In the Singleton pattern (http://it.wikipedia.org/wiki/Singleton) a static method is necessary to retrieve the Singleton's instance (also a private static attribute is necessary to keep track of it).
For those who claim Singleton are evil and you should always use Dependency Injection via Google Guice, also Guice relies on static method for instance to create an injector (http://lowcoupling.wordpress.com/2012/12/05/dependency-injection/).
So I guess the best answer is you should always think if the problem you are facing might just be solved by the injection of object but there are cases in which the usage of static methods is pretty reasonable.
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; }.
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