1. What is the difference between an Interface and Abstract class ?
· A class may implement several interfaces where as it can extend only one abstract class.
· An interface cannot provide any code at all, much less default code where as an abstract class can provide complete code, default code, and/or just stubs that has to be overridden.
· Static final constants can use the interfaces without qualification in classes that implement the interface where as both static constants and instance variables can use the abstract classes.
· Interface implementation can be added to any existing third part classes where as third party classes must be rewritten to extend from the abstract classes.
· Interfaces are used to often represent peripheral ability of a class, not central identity where as abstract class defines core identity of its descendants.
· If various implementations share is method signature then Interface works best where as If the various implementations are all of a kind and share a common status and behavior, usually an abstract class works best.
· Interface is slow where as abstract class is fast in performance.
· If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method
where as in case of an abstract class if you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.
· You can put shared code into an abstract class, where you cannot into an interface.
2. How do you write user-defined exceptions in Java?
· To write user-defined exception in java, you exception class should extend Exception base class have call the super class methods in your own methods.
3. What is multiple inheritance? Is it supported in java?
· This means one object having multiple parents, which is not truly supported in java but supported in C++. But overall design of java suggests that we can implement multiple inheritances in java using interfaces.
4. What is difference between an inner class and static inner class?
· A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.
5. What is an inner class and what is its advantage?
· Inner class is class, which is defined inside a class as private class and always bears a reference to the outer class.
o Inner classes can be nested within the body of a method.
· Scope of inner class is the entire enclosing class in which the inner class is nested.
· Inner classes can access attributes and methods in nesting class.
· Each inner class is compiled into a separate. class file labeled:NestingClass$InnerClass.
· Inner classes can contain methods that return handles to inner class instances.
· Major advantage of inner classes is the ability to create adaptor classes that implement an interface.
o Make all inner classes private to ensure hidden implementation.
o Rather than handle classes returning inner classes, they return interfaces.
· Inner classes frequently used with event handling in applets.
· A class may implement several interfaces where as it can extend only one abstract class.
· An interface cannot provide any code at all, much less default code where as an abstract class can provide complete code, default code, and/or just stubs that has to be overridden.
· Static final constants can use the interfaces without qualification in classes that implement the interface where as both static constants and instance variables can use the abstract classes.
· Interface implementation can be added to any existing third part classes where as third party classes must be rewritten to extend from the abstract classes.
· Interfaces are used to often represent peripheral ability of a class, not central identity where as abstract class defines core identity of its descendants.
· If various implementations share is method signature then Interface works best where as If the various implementations are all of a kind and share a common status and behavior, usually an abstract class works best.
· Interface is slow where as abstract class is fast in performance.
· If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method
where as in case of an abstract class if you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.
· You can put shared code into an abstract class, where you cannot into an interface.
2. How do you write user-defined exceptions in Java?
· To write user-defined exception in java, you exception class should extend Exception base class have call the super class methods in your own methods.
3. What is multiple inheritance? Is it supported in java?
· This means one object having multiple parents, which is not truly supported in java but supported in C++. But overall design of java suggests that we can implement multiple inheritances in java using interfaces.
4. What is difference between an inner class and static inner class?
· A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.
5. What is an inner class and what is its advantage?
· Inner class is class, which is defined inside a class as private class and always bears a reference to the outer class.
o Inner classes can be nested within the body of a method.
· Scope of inner class is the entire enclosing class in which the inner class is nested.
· Inner classes can access attributes and methods in nesting class.
· Each inner class is compiled into a separate. class file labeled:NestingClass$InnerClass.
· Inner classes can contain methods that return handles to inner class instances.
· Major advantage of inner classes is the ability to create adaptor classes that implement an interface.
o Make all inner classes private to ensure hidden implementation.
o Rather than handle classes returning inner classes, they return interfaces.
· Inner classes frequently used with event handling in applets.
0 comments:
Post a Comment