Yes, an abstract class can have a constructor. Consider this: abstract class Product { int multiplyBy ; public Product ( int multiplyBy ) { this . multiplyBy = multiplyBy ; } public int mutiply ( int val ) { return multiplyBy * val ; } } class TimesTwo extends Product { public TimesTwo () { super ( 2 ); } } class TimesWhat extends Product { public TimesWhat ( int what ) { super ( what ); } } The superclass Product is abstract and has a constructor. The concrete class TimesTwo has a constructor that just hardcodes the value 2. The concrete class TimesWhat has a constructor that allows the caller to specify the value. NOTE: As there is no default (or no-arg) constructor in the parent abstract class the constructor used in subclasses must be specified. Abstract constructors will frequently be used to enforce class constraints or invariants such as the minimum field...
public static void main(String[] args) { add(5,6); // compile error because of ambiguous state add((int)5, (long)6); //works fine and output is "in int case" add((long)5, (int)6); //works fine and output is "in long case" } public static void add(int a,long b){ System.out.println("in int case"); } public static void add(long b,int a){ System.out.println("in long case"); } }
The question is in Java why can't I define an abstract static method? for example abstract class foo { abstract void bar ( ); // <-- this is ok abstract static void bar2 (); //<-- this isn't why? } Ans: Because "abstract" means: "Implements no functionality", and "static" means: "There is functionality even if you don't have an object instance". And that's a logical contradiction.
Why does Java allow me to call static methods on instances? Your assumption is wrong. It never calls on the instance of the class. It always called on class. Try below sample code and you will never get NullPointerException class ABC { public static void hello () { System . out . println ( "Hello" ); } } ABC abc = null ; abc . hello (); What would have gone wrong if Java would have supported Runtime Polymorphism for static methods? Polymorphism comes into picture when you override the method in subclass. since static method belong to class hence there is no meaning of overriding static methods. Hence Polymorphism always works for instance methods only that belongs to the instances of the class. Source: Stackoverflow
String x = ( String ) null ; Why there is no exception in this statement? String x = null ; System . out . println ( x ); It prints null . But .toString() method should throw a null pointer exception. Ans: You can cast null to any reference type without getting any exception. println method does not throw null pointer because it first checks whether the object is null or not. If null then it simply prints the string "null" . Otherwise it will call the toString method of that object. Adding more details: Internally print methods call String.valueOf(object) method on the input object. And in valueOf method, this check helps to avoid null pointer excpeiton: return ( obj == null ) ? "null" : obj . toString (); For rest of your confusion, calling any method on a null object should throw a null pointer exception, if not a special case. Source: Stackoverflow ------------------...
The job of the DispatcherServlet is to take an incoming URI and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource that's supposed to be found at that location. I might have a file /WEB-INF/jsp/pages/Home.jsp and a method on a class @RequestMapping ( value = "/pages/Home.html" ) private ModelMap buildHome () { return somestuff ; } The Dispatcher servlet is the bit that "knows" to call that method when a browser requests the page, and to combine its results with the matching JSP file to make an html document. How it accomplishes this varies widely with configuration and Spring version. There's also no reason the end result has to be web pages. It can do the same thing to locate RMI end points, handle SOAP requests, anything that can come into a servlet. ------------------------------------...
JVM Architecture Following diagram summarizes the key components in a JVM. In the JVM architecture, two main components that are related to garbage collection are heap memory and garbage collector. Heap memory is the runtime data area where the instances will be store and the garbage collector will operate on. Now we know how these things fit in the larger scheme. Java Heap Memory It is essential to understand the role of heap memory in JVM memory model. At runtime the Java instances are stored in the heap memory area. When an object is not referenced anymore it becomes eligible for eviction from heap memory. During garbage collection process, those objects are evicted from heap memory and the space is reclaimed. Heap memory has three major areas, Young Generation Eden Space (any instance enters the runtime memory area through eden) S0 Survivor Space (older instances moved from eden to S0) S1 Survivor Space (older instances moved from S0 to S1) Old Generation (instances...