Some keypoints in Java

Restrictions for static method
   There are two main restrictions for the static method. They are:
    1) The static method can not use non static data member or call non-static method directly.
    2) 'this' and 'super' cannot be used in static context.

Why java main method is static?
  Because object is not required to call a static method.If it were non-static method, jvm would create object first then call main() method that will lead the problem of extra memory allocation.

Ways to copy the values of one object into another in java.
    1) By constructor
    2) By assigning the values of one object into another
    3) By clone() method of Object class

Why multiple inheritance is not supported in java?
 To reduce the complexity and to simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class. In c++ it leads to "diamond problem" which is resolved using 'virtual' keyword. In java interface comes to rescue.

There are two ways to overload a method in java
  1) By changing number of arguments
  2) By changing the data type




 Can we overload java main() method?
  Yes, you can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only.

Can we override static method?
No, static method cannot be overridden,because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.
Thence, main() method cannot be overridden.

Final keyword
 1) Final variable cannot be altered
 2) Final method cannot be overridden
 3) Final class cannot be inherited

Abstraction in Java
 1) Class (0% )
 2) Abstract class (0 - 100%)
 3) Interface ( 100%)

Abstract class rules:
 1) If there is any abstract method in a class, that class must be abstract.
 2) If you are extending any abstract class that have abstract method, you must either provide the implementation of the method or make this class abstract
3) It cannot be instantiated


Which should you use, abstract classes or interfaces?
    Consider using abstract classes if any of these statements apply to your situation:
        You want to share code among several closely related classes.
        You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
        You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

    Consider using interfaces if any of these statements apply to your situation:
        You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
        You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
        You want to take advantage of multiple inheritance of type.














Comments

Popular Posts