Simple core Java fundamentals
What is an abstract method?
A method that has no implementation and which is expected to be implemented by a subclass is called an abstract method.What is an abstract class?
A class containing atleast one abstract method is called an abstract class. A method that has no implementation and which is expected to be implemented by a subclass is called an abstract method. An Abstract class cannot be instantiated. It is expected to be extended by a subclass. An abstract class may contain static variables declared. Any class with an abstract method must be declared explicitly as abstract. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.How can you invoke a defined method of an abstract class?
Java interface
A java class containing all the methods as abstract is called an interface. A method that has no implementation and which is expected to be implemented by a subclass is called an abstract method. Java interface can contain constants. When an interface needs to be instantiated it should be implemented by a class and all its abstract methods should be defined. If all the methods are not implemented in the class then it becomes a java abstract class and so cannot be instantiated. Variables declared in a java interface by default is final.When can an object reference be cast to a Java interface reference?
Difference Between Interface and Abstract Class
- Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
- Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
- Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
- Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
- An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
- A Java class can implement multiple interfaces but it can extend only one abstract class.
- Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
- In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
Does Java garbage collection guarantee that a program will not run out of memory?
No. Java Programs may use up memory resources faster than they are garbage collected. A Java program can create objects that are not subject to garbage collection.Java garbage collection
Java finalization
Finalization is to give an unreachable Java object the opportunity to perform any cleanup processing before the Java object is garbage collected. It happens when the Java object’s finalize() method is invoked.Java Pass By Value and Pass By Reference.
With Java objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same Java object. Java primitives too are passed by value.
Can an unreachable Java object become reachable again?
Yes. It can happen when the Java object’s finalize() method is invoked and the Java object performs an operation which causes it to become accessible to reachable objects.When does the finally clause in java exception block never executes?
How can the user achieve that in Java?
Include “System.exit(1);” before the finally block and stop the execution flow of the java program.
0 comments:
Post a Comment