constructor and modifier - mehnazchaudary9595/java-learning GitHub Wiki
constructor
A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined value Constructors cannot be inherited. constructor function has the same name as the class. A class may also have a destructor function to destroy objects of that class. Rules for creating java constructo rules for creating java constructor
There are basically two rules defined for the constructor.
1.Constructor name must be same as its class name
2.Constructor must have no explicit return type
Types of java constructors
There are two types of constructors:
1.Default constructor (no-arg constructor) Default constructor provides the default values to the object like 0, null etc. depending on the type.
2.Parameterized constructor
constructor that have parameters is known as parameterized constructor
what is modifier in java?
Modifiers are keywords that you add to those definitions to change their meanings. The Java language has a wide variety of modifiers, including the following:
1.Java Access Modifiers
2.Non Access Modifiers
To use a modifier, you include its keyword in the definition of a class, method, or variable. The modifier precedes the rest of the statement.
Access Control Modifiers:
Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:
1.Visible to the package, the default. No modifiers are needed.
2.Visible to the class only (private).
3.Visible to the world (public).
4.Visible to the package and all subclasses (protected).
Non Access Modifiers:
Java provides a number of non-access modifiers to achieve many other functionality.
1.The static modifier for creating class methods and variables
2.The final modifier for finalizing the implementations of classes, methods, and variables.
3.The abstract modifier for creating abstract classes and methods.
4.The synchronized and volatile modifiers, which are used for threads.