oops basic questions Part-4
Constructors must have the same name of the class and cannot have a return type. They are called only once, while regular methods can be called whenever required. We cannot explicitly call a constructor.How can you call a constructor from another constructor? By using this() reference. How can you call the constructor of super class? By using super() syntax. What is constructor chaining?
When a constructor of a class is executed it will automatically call the default constructor of the super class (if no explicit call to any of the super class constructor) and soon till the root of the hierarchy this is called constructor chaining.When does a compiler supply a default constructor for a class? If a class doesn't have a constructor, the JVM will provide a default constructor for the class. Overriding Overloading What is method overloading? Method overloading is the process of creating a new method with the same name and with different signature or argument list. What is method overriding? Method overriding is the process of giving a new implementation in the child class for an existing method of parent class by using same method name signature. What are the restrictions placed on overriding a method? The overriding method must have the same argument list Overriding a method must have same return type. The access specified cannot be more restrictive than the super class method.
We cannot throw any new exceptions or border checked exception in overriding method may throw fewer or narrower checked exceptions, or any unchecked exception. Final methods cannot be overriddenWhat are the restrictions placed on overloading a method? Overloading methods must differ in their parameter list, or number of parameters. Why we cannot override static methods? Static means they are associated with a class. In static methods, the binding mechanism is static binding. So it must be available at the compile time. What is casting? Casting means converting one type to another. There are mainly two types of casting. Casting between primitive types and casting between object references. Casting between primitive numeric types is used to convert larger data types to smaller data types. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference. What is numeric promotion? Numeric promotion is the conversion of a smaller numeric type to a larger numeric type. In numerical promotion, byte, char, and short values are converted to int values. The int, long and float values are converted to the desired types if required. What is a void return type ? A void indicates that the method will not return anything. Why java is said to be pass-by-value? When assigning an object to a variable, we are actually assigning the memory address of that object to the variable. So the value passed is actually the memory location of the object. These results in object aliasing, meaning you can have many variables referring to the same object on the heap. What is a package? To group set of classes into a single unit is known as packaging. Packages provide wide namespace visibility. What is the use of packages in java? Packages are a way to organize files in java when a project consists of more than one module. It helps in resolving name conflicts when different modules have classes with the same names.