Abstract class and interface
Q #23) Difference between Abstract class and Interface.
Ans: Difference between Abstract Class and Interface are as follows:
Abstract Class:
- Abstract classes have a default constructor and it is called whenever the concrete subclass is instantiated.
- Contains Abstract methods as well as Non-Abstract methods.
- The class which extends the Abstract class shouldn’t require implementing all the methods, only Abstract methods need to be implemented in the concrete sub-class.
- Abstract Class contains instance variables.
Interface:
- Doesn’t have any constructor and couldn’t be instantiated.
- Abstract method alone should be declared.
- Classes which implement the interface should provide the implementation for all the methods.
- The interface contains only constants.
Q #5) Interface?
- All the methods in the interface are internally public abstract void.
- All the variables in the interface are internally public static final that is constants.
- Classes can implement the interface and not extends.
- The class which implements the interface should provide an implementation for all the methods declared in the interface.
Q #6) Abstract Class?
- An abstract class may have a Non- abstract method also.
What is the use of main method in abstract class?
Loading a class is not the same as creating an instance of the class. And there's no need to create an instance of the class to call main(), because it's static. So there's no problem.
Abstract just means you can't instantiate the class directly. You can have constructors if you want - they might be needed for subclasses to initiate the object state. You can have static methods, including main() and they don't need an object so calling them is fine.
Loading a class is not the same as creating an instance of the class. And there's no need to create an instance of the class to call main(), because it's static. So there's no problem.
Can abstract class has static member?
Static Variables can be used in an abstract class to define constants that could be used througout the class hierarchy.
Java 8
1. Interface
Interface is a blueprint for your class that can be used to implement a class ( abstract or not); the point is interface cannot have any concrete methods. Concrete methods are those methods which have some code inside them; in one word - implemented. What your interface can have is static members and method signatures. The example below shall help you understand how to write an interface.
public interface Brain{
public static final int number = 1;
public void talk( String name );
public abstract void doProgramming();
}
The declaration is much like a class but inside the interface there are some strict rules you need to follow:
- All methods that you declare in an interface can have ‘ static ’, ‘ default ’ or ‘ abstract ’ modifiers ( Since Java 8 ). Implicitly they are ‘ public abstract ’.
- Since Java 8, methods can be implemented ( can have a code body ) in an interface if only if it is declared static or default. Abstract methods cannot have a body; all they can have is a method signature as shown in the example above.
- Variables are not allowed in interface. Hence any data declaration is ‘ public static final ’; hence only constants.
- Interfaces can extend other interfaces ( one or more ) but not classes ( abstract or not ).
- Interfaces cannot be instantiated as they are not concrete classes.
- Methods and constants cannot be declared ‘ private ’, methods cannot be declared ‘ final ’.
2. Abstract class
Abstract classes are a bit different from interfaces. These are also used to create blueprints for concrete classes but abstract classes may have implemented methods. But to qualify as an abstract class, it must have at least one abstract method. Abstract classes can implement one or more interfaces and can extend one abstract class at most. There is a logical reason to this design which we will talk about later in this post. Here is an example of Abstract class creation.
public abstract class Car{
public static final int wheels = 4;
String turn( String direction ){
System.out.println( "Turning" + direction );
}
public abstract void startWithSound( String sound );
public abstract void shutdown( );
}
The declaration rules are as follows:
- A class can be an abstract class without having any methods inside it. But if it has any methods inside it, it must have at least one abstract method. This rule does not apply to static methods.
- As abstract classes can have both abstract and non abstract methods, hence the abstract modifier is necessary here ( unlike in interface where only abstract methods are allowed ).
- Static members are allowed.
- Abstract classes can extend other at most one abstract or concrete class and implement several interfaces.
- Any class that does not implement all the abstract methods of it’s super class has to be an abstract class itself.
What is Marker interface?
A marker interface is an empty interface without any method but used to force some functionality in implementing classes by Java. Some of the well known marker interfaces are Serializable and Cloneable.
Explain marker interfaces?
The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata. In java, it is used as interfaces with no method specified.
A good example of use of marker interface in java is Serializable interface. A class implements this interface to indicate that its non-transient data members can be written to a byte steam or file system.
A major problem with marker interfaces is that an interface defines a contract for implementing classes, and that contract is inherited by all subclasses. This means that you cannot “un-implement” a marker. In the example given, if you create a subclass that you do not want to serialize (perhaps because it depends on transient state), you must resort to explicitly throwing NotSerializableException.
The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata. In java, it is used as interfaces with no method specified.
A good example of use of marker interface in java is Serializable interface. A class implements this interface to indicate that its non-transient data members can be written to a byte steam or file system.
A major problem with marker interfaces is that an interface defines a contract for implementing classes, and that contract is inherited by all subclasses. This means that you cannot “un-implement” a marker. In the example given, if you create a subclass that you do not want to serialize (perhaps because it depends on transient state), you must resort to explicitly throwing NotSerializableException.
No comments:
Post a Comment