- What is Constructor?
- A constructor is a special method that is used to initialize an object of the class.
- It is special because its name is the same as the class name.
- They do not have return types, not even void and therefore they cannot return values.
- They cannot be inherited, though a derived class can call the base class constructor.
- Constructor is invoked whenever an object of its associated class is created.
- How does the Java default constructor be provided?
If a class defined by the code does not have any constructor, compiler will automatically provide default-constructor for the class. The access modifier of the default constructor is the same as the class itself.
- Can constructor be inherited?
No, constructor cannot be inherited, but it can be called from the derived class constructor by using super Keyword.
- What are the differences between Constructors and Methods?
|Create an instance of a class||Group Java statements|
|Cannot be abstract, final, native, static, or synchronized||Can be abstract, final, native, static, or synchronized|
|No return type, not even void||void or a valid return type|
|Same name as the class .||Any name except the class. Method names begin with a lowercase letter by convention .|
|this keyword :-Refers to another constructor in the same class. If used, it must be the first line of the constructor||this keyword :-Refers to an instance of the owning class. Cannot be used by static methods.|
|Super keyword :-Calls the constructor of the parent class. If used, must be the first line of the constructor||Super keyword :-Calls an overridden method in the parent class|
|Constructors are not inherited||Methods are inherited|
- How are this () and super () used with constructors?
- Constructors use this to refer to another constructor in the same class with a different parameter list. It must be the first line of the constructor.
- Constructors use super to invoke the super class’s constructor. If a constructor uses super, it must use it in the first line; otherwise, the compiler will complain.
- What are the differences between Class Methods and Instance Methods?
|Class Methods||Instance Methods|
|Class methods are methods which are declared as static. The method can be called without creating an instance of the class||Instance methods on the other hand require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword.
Instance methods operate on specific instances of classes.
|Class methods can only operate on class members and not on instance members as class methods are unaware of instance members.||Instance methods of the class can also not be called from within a class method unless they are being called on an instance of that class.|
|Class methods are methods which are declared as static. The method can be called without creating an instance of the class.||Instance methods are not declared as static.|
- What are Access Specifiers?
Access specifiers, specify the access to our code by other classes – whether other classes can access or not and if permitted, to what extent they can access. Access Specifiers supports encapsulation in Java.
Access specifiers are used to control the visibility of members like classes, variables and methods. There are four access specifiers: public, private and protected.
- What are Access Specifiers available in Java?
Java offers four access specifiers, listed below in decreasing accessibility:
- Public– public classes, methods, and fields can be accessed from everywhere.
- Protected– protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.
- Default:-If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.
- Private– Private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within sub classes and are not inherited by sub classes.
- What is final modifier?
The final modifier keyword makes that the programmer cannot change the value anymore. The actual meaning depends on whether it is applied to a class, a variable, or a method.
- final Classes– A final class cannot have subclass.
- final Variables– A final variable cannot be changed once it is initialized.
- final Methods– A final method cannot be overridden by sub classes.