[Tech.][Struts.][Servlet.][JSP.][EJB.][JNDI-JMS][SQL.][JDBC.]
CORE-JAVA: [OOP][CLASS][ABSTRACT][EXCEPTIONS][THREADS][UTILS][PACKAGES][JVM][CASTING][NETWORKING][RMI-XML]
* What is the difference between an Interface and an Abstract class?
* [INTERFACE] An Interface can only declare constants and instance methods, but cannot implement default behavior.
All the methods and properties defined in Interface are by default public and abstract.
Interfaces are recommended to be used when something in design will change frequently
We use abstract class and Interface for the base class in our application.
An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.
* [ABSTRACT] An abstract class can have instance methods that implement a default behavior.
An abstract class is a class that is declared abstract.
It may or may not include abstract methods.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon). Ex: abstract void moveTo(double deltaX, double deltaY);
Abstract classes cannot be instantiated.
Abstract classes can only be subclassed.
If a class includes abstract methods, the class itself must be declared abstract.
Ex: public abstract class GraphicObject {
// declare fields and non-abstract methods
abstract void moveTo(double deltaX, double deltaY);
abstract void draw(); } (compile online)
An abstract class may have the usual flavors of class members (private, protected, etc.)
* What modifiers are allowed for methods in an Interface? (Interface)
A: Only public and abstract modifiers are allowed for methods in interfaces.
* What is meant by "Abstract Interface"? (Interface)
A: First, an interface is abstract. That means you cannot have any implementation in an interface. All the methods declared in an interface are abstract methods or signatures of the methods.
Why it is not recommended to have instance variables in Interface. (interface)
By Default, All data members and methods in an Interface are public. Having public variables in a class that will be implementing it will be violation of the Encapsulation principal. Although its possible to have variables.
* What are the characteristics of an interface? (interface)
An interface can extend any number of interfaces. (Ex.)
public interface GroupedInterface extends Interface1, Interface2, Interface3 {
--// constant declarations are implicitly public, static, and final. (cannot be private)
--double E = 2.718282; // base of natural logarithms
--// method signatures are implicitly public (cannot be private)
--void doSomething (int i, double x);
--int doSomethingElse(String s); } (compile online)
If access specifier is not indicated before an interface as public, then by default the interface will be accessible only to classes defined in the same package as the interface.
All methods declared in an interface are implicitly public, thus the public modifier can be omitted.
An interface can contain constant declarations. All constant values defined in an interface are implicitly public, static, and final. Thus, specifying these modifiers in the code can be omitted. Interfaces can have inner classes.
What must a class do to implement an interface? (interface)
It MUST implement all of the methods of the interface and identify the interface in its implements clause. All methods defined by that interface must appear in its source code.
What modifiers may be used with an interface declaration? (interface)
An interface may be declared as public or abstract or default (no modifier).
Can an Interface be final? (interface)
No, an Interface cannot be final, private or protected. It can only be either public, abstract or default (when access is not specified). Public is available in any package, and default is in the same package.
Can an Interface have an inner class? (interface)
Yes it can. Ex:
public interface abc
{ static int i=0; void dd(); // constant variable and public method.
class a1 // it cannot be private
-{
--a1()
---{ int j; System.out.println("inside"); };
--public static void main(String a1[])
---{ System.out.println("in interfia"); }
-}
} (compile online)
* What is an abstract class? (abstract)
A: Abstract class MUST be extended/subclassed. It serves as a template.
An abstract class must NOT be instantiated (ie, you may not call its constructor)
Abstract class must NOT be declared as PRIVATE or FINAL.
An abstract class MAY contain PRIVATE and STATIC data.
Any class with an abstract method MUST be declared as abstract.
An abstract method must NOT be PRIVATE or STATIC.
An NON-abstract method MAY be PRIVATE STATIC or public.
This prevents it from being instantiated but non-abstract methods can be used in its inherited subclasses. (Ex:)
abstract class Abstract1 { // cannot be private
--private static int x, y; // private static variable.
--private static void moveTo(int newX, int newY) { // private static method.
---System.out.println("in abstract class, non-abstract method"); }
--abstract void draw(); // cannot be PRIVATE or STATIC.
} (compile online)
Note: non-abstract methods can be static but not abstract methods.
* Can we make an instance of an abstract class? For example - java.util.Calender is an abstract class with a method getInstance() which returns an instance of the Calender class. (abstract)
A: No! We cannot make an instance of an abstract class. An abstract class has to be sub-classed. If we have an abstract class and we want to use a method which has been implemented, we may need to subclass that abstract class, instantiate our subclass and then call that method.
Can an abstract class be final? (abstract)
An abstract class may not be declared as final NOR private.
Can there be an abstract class with no abstract methods in it? (abstract)
Yes, it is possible.
What is an abstract method? (abstract)
An abstract method is a method whose implementation is deferred to a subclass.
What does the "abstract" keyword mean in front of a method? A class? (abstract)
Abstract keyword declares either a method or a class.
Abstract method may have only arguments and return types.
Abstract methods act as placeholder methods that are implemented in the subclasses.
Abstract classes can't be instantiated.
If a class contains any abstract method it must be declared as abstract.
What does it mean that a method or class is abstract? (abstract)
An abstract class cannot be instantiated. Only its subclasses can be instantiated. You indicate that a class is abstract with the abstract keyword like this:
public abstract class Container extends Component {
Abstract classes may contain abstract methods. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. It has no body. For example,
public abstract float price();
Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do. Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract.
* Can an ABSTRACT class implement an INTERFACE?
Yes we can! It is possible, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract. For example:
abstract class X implements Y { // implements all but one method of Y }
class XX extends X { // implements the remaining method in Y }
Note that class XX is not abstract, thus it has to implement all methods of Y.
Can an nested Anonymous class be declared as implementing an interface and extending a class? (interface) (nested)
An anonymous class may implement an interface OR extend a superclass, but may NOT be declared to do BOTH.
* How can we take decision about when we have to use Interface and when Abstract Class? When should we use both? Interfaces and abstract classes seem superficially to provide almost the same capability. How do we decide which to use?
* ABSTRACT class is a abstract view of any real word entity and INTERFACE is a more abstract one. When we thinking about the entity there are two things one is intention and one is implementation. Intention is we know about the entity and also may have the idea about its state as well as behavior but don't know about how its looks or works or may know partially. Implementation means actual state and behavior of entity.
Consider this example. In a Content Management System where CONTENT is a generalize form of ARTICLES, REVIEWS, BLOGS etc.
So CONTENT is our base class now we have to decide whether CONTENT class should be an ABSTRACT class, INTERFACE or REGULAR class.
Consider REGULAR class vs other types (abstract and interface). If CONTENT is not a core entity of this application. As per the business logic if ARTICLES, BLOGS and REVIEWS are the core parts of business logic, then CONTENT class should not be a REGULAR class because we will never make instance of that class. Thus ABSTRACT class and INTERFACE are the more appropriate choices.
Between Interface and Abstract Class.
As we can see CONTENT having behavior method named PUBLISH. According to our business logic if PUBLISH is having some default behavior which is applicable to other classes then we prefer CONTENT class as an ABSTRACT class. If there is no default behavior for the PUBLISH and if every drive class makes its own implementation, then there is no need to implement PUBLISH behavior in the base class, thus we prefer INTERFACE. These are the in general ideas of deciding between ABSTRACT class, INTERFACE and REGULAR class.
If we make CONTENT class as INTERFACE then it is difficult to make changes in the base class, as if we add a new method or property in CONTENT interface then we may have to implement this new method in every drive class. These problems may be avoided if we are using ABSTRACT CONTENT class and the new method is not of an ABSTRACT type. So we can replace INTERFACE with ABSTRACT class. This will restrict the multiple inheritance of the base class.
CAN-DO and IS-A relationship will also define the difference between INTERFACE and ABSTRACT class. As discuss INTERFACE can be use for multiple inheritance. For example if we have another INTERFACE named ICOPY which is having a behavior method named COPY and if every drive class has to implement its own implementation of COPY. If ARTICLE class is derived from ABSTRACT class CONTENT as well as ICOPY then ARTICLE "CAN-DO" COPY also.
IS-A is for "generalization" and "specialization". If CONTENT is a generalized form of Articles, Blogs, Reviews and if Articles, Blogs, Reviews are a specialized forms of CONTENT. So, ABSTRACT class defines core identity. If we are thinking in term of speed then ABSTRACT is quicker then INTERFACE, as interface requires extra in-direction.
As per the example, as ABSTRACT class is having upper-hand if compared to an INTERFACE. The only advantage of INTERFACE is multiple inheritance.
An INTERFACE facilitates us to start from scratch to implement our interface in some other code whose original/primary purpose was quite different from our interface. For others, our interface is only incidental, something that has to be added on to the their code to use our package.
An ABSTRACT class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. It may be inconvenient to extent the class if the programmers using our package who have already developed the class hierarchy. As in Java, a class can inherit from only one base class.
*
[ Religion of the Universe.][The piercing-of-the-ears-(Karnavedha).][Nature-Cure/Therapy.]
"WE" has More Power than "I". Please leave a comment if you encounter any problems or have any suggestions.
The Bitter Truth.
Duty, Responsibility, Law and Order. "Happy Kingdom" proudly proclaimed as "Paradise" Trying to learn something for the first time. Time is the only solution. For every Action there is an Equal and Opposite Reaction. Logic of Good and Bad. Duties, Responsibilities verses Luxuries and Pleasures. Beggars!
No comments:
Post a Comment