1Class c = Class.forName("packageName.MyClass"); 2
3 MyClass object = (MyClass) c.newInstance();
b) Using clone() method. 1 MyClass object1 = new MyClass(); 2 3 MyClass object2 = object1.clone(); c) Using object deserialization 1 ObjectInputStream inStream = new ObjectInputStream(anInputStream ); 2 3 MyClass object = (MyClass) inStream.readObject(); MyClass object = (MyClass) inStream.readObject(); d) Creating string and array objects 1 String s = "string object"; 2 3 int[] a = {1, 2, 3, 4};
These are the modifiers which are used to restrict the visibility of a class or a field or a method or a constructor. Java supports 4 access modifiers. a) private : private fields or methods or constructors are visible within the class in which they are defined. b) protected : Protected members of a class are visible within the package but they can be inherited to sub classes outside the package. c) public : public members are visible everywhere. d) default or No-access modifiers : Members of a class which are defined with no access modifiers are visible within the package in which they are defined. (For more info on access modifiers, click here.)
These are the modifiers which are used to achieve the functionalities other than the accessibility. For example, a) static : This modifier is used to specify whether a member is a class member or an instance member. b) final : It is used to restrict the further modification of a class or a method or a field. (for more on final, click here). c) abstract : abstract class or abstract method must be enhanced or modified further. (For more on abstract, click here). d) synchronized : It is used to achieve thread safeness. Only one thread can execute a method or a block which is declared as synchronized at any given time. (for more on synchronized, click here.) (For more info on access Vs non-access modifiers, click here)
Yes, first int is auto-widened to double and then double is auto-boxed to Double. 1 double d = 10; //auto-widening from int to double 2 3 Double D = d; //auto-boxing from double to Double
1 public class A 2 { 3 public A() 4 { 5 //-----> (1) 6 } 7 8 void A() 9 { 10 //-----> (2) 11 } 12 }None of them. It is neither constructor overloaded nor method overloaded. First one is a constructor and second one is a method.
There are 5 main rules you should kept in mind while overriding a method. They are, a) Name of the method must be same as that of super class method. b) Return type of overridden method must be compatible with the method being overridden. i.e if a method has primitive type as it’s return type then it must be overridden with primitive type only and if a method has derived type as it’s return type then it must be overridden with same type or it’s sub class types. c) You must not reduce the visibility of a method while overriding. d) You must not change parameter list of a method while overriding. e) You can not increase the scope of exceptions while overriding a method with throws clause.