![]() ![]() However, based on your question first edit, you could define a field in an interface, however there is a caveat. Interfaces Comparable and Cloneable are implemented by many unrelated classes.Ģ.You want to specify the behavior of a particular data type, but not concerned about whoģ.You want to take advantage of multiple inheritance of type. This enables you to define methods that can access and modify the state of the object to which they belong.Ĭonsider using interfaces if any of these statements apply to your situation:ġ.You expect that unrelated classes would implement your interface. You want to declare non-static or non-final fields.Methods or fields, or require access modifiers other than public (such as protected and private). You expect that classes that extend your abstract class have many common.You want to share code among several closely related classes.It can be said as object referenced variables via interface. But can show the same purpose when implementing an interface as the interface object/instance is made is of class type. Which should you use, abstract classes or interfaces?Ĭonsider using abstract classes if any of these statements apply to your situation: Interface doesnt hold by itself instance variables of its own as by default inside interface variables are static and final. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. If you look up the java docs, you will get the actual statements from there.Ībstract classes are similar to interfaces. This gives you the flexibility of using an Interface type while still being able to share implementation details via a non-instantiable abstract class across the group of concrete classes that you want. IModifier imod = new SpecificModifier("MyName") I am trying to do something like this: public interface IModifierĪnd a declaration. Ignore everything up until the second edit ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |