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?

An abstract class cannot be instantiated directly. An abstract class has to be sub-classed first and then instantiated. Only then the method defined in the abstract class can be invoked.

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?

When a Java object implements the referenced interface it can be cast to the Java interface reference.

Difference Between Interface and Abstract Class

  1. 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.
  2. Variables declared in a Java interface is by default final. An  abstract class may contain non-final variables.
  3. 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..
  4. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
  5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
  6. A Java class can implement multiple interfaces but it can extend only one abstract class.
  7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
  8. 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

Garbage collection in Java is to discard objects that are no longer needed and to reclaim their resources. A Java object is subject to garbage collection when it is out of scope of the control flow of the program.

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.

Pass by value in java means passing a copy of the value to be passed. Pass by reference in java means the passing the address itself. In Java the arguments are always passed by value. Java only supports pass by value.
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?

The finally clause in the try-catch exeception block always executes, irrespective of the occurence of exeception. This is applicable for the normal java program flow. If the execution flow is stopped irreversibly before the finally clause, then the finally block will not be executed.
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.

Can a java subclass declare a private method available in its java superclass?

Yes. A java private member cannot be inherited as it is available only to the declared java class. Since the private members cannot be inherited, there is no place for discussion on java runtime overloading or java overriding (polymorphism) features.

Difference between static and non-static java inner class.

A static java inner class cannot have instances. A non-static java inner class can have instances that belong to the outer class.



0 comments:

Post a Comment