Assignment # 2 - Rozeenaa/LearningJava GitHub Wiki
# What are Modifiers in Java?
Modifiers are keywords that you add to these definations 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 defination of a cl;ass, method, or variable. The modifier preceds the rest of the statement as in the following example.
`Public Class classname{``
//...
}
Private boolean myflag;
static final double weeks= 9.5;
protected static void main (string [] arguments) {
// body of method
}
# 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 :
- Visible to the package, the default.No modifiers are needed.
- Visible to the class only (Private) .
- Visible to the world (Public).
- Visible to the package and all subclasses (Protected).
# Non Access Modifiers:
Java provides a number of non-access modifiers to achieve many other functionality.
- The Static modifier for creating class methods and variables.
- The Final modifier for finalizing the implementation of classes, methods and variables.
- The Abstract modifier for creating abstract classes and methods.
- The Synchronized and volatile modifiers which are used for threads.
# Constructors in Java
Constructors in java is special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It construct the values i.e. provide data for the object that is why it is known as Constructor.
# Rules for creating Java constructor:
There are basically two rules defined for the constructors.
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 java constructors.
1. Default Constructor (no-arg constructors)
2. Parameterized Constructor
# Java Default Constructors:
A constructor that has no parameter is known as default constructor.
# Syntax of default constructor
class-name(){}
# Example of default constructor
In this example we are creatinng the no-arg constructor in the Bike class. It will be invokes at the time of object creation.
Class Bike1{
Bike1(){
system.out.println("Bike is created");}
Public static void main(string args[]){
Bike1 b=new Bike1();
}
}
# Java Parameterized Constructor
A constructor that have parameter is known as parameterized constructor.
# Example of parameterized constructor
In this example we have created the constructor of student class that have two parameters. We can have any number of parameters in the constructor.
class student4{
int id;
string name;
student4(int i,string n){
id=i;
name=n;
}
void display(){
system.out.println(id+" "+name):}
Public static void main(string args[]){
student4 s1=new student4(111,"Karan");
student4 s2=new student4(222,"Aryan");
s1.display();
s2.display();
}
}