ASSIGNMENT # 2 - TahiraKiran456/Learning-Java GitHub Wiki
#what is modifire in java?
Modifiers are keywords that are added to change meaning of a definition. In Java, modifiers are catagorized into two types,
1) Access control modifier
2)Non Access Modifier
-
Access control modifier:
Java language has four access modifier to control access levels for classes, variable methods and constructor.
1)Default : Default has scope only inside the same package 2)Public : Public scope is visible everywhere 3)Protected : Protected has scope within the package and all sub classes 4)Private : Private has scope only within the classes.
- Non-access Modifier: Non-access modifiers do not change the accessibility of variables and methods, but they do provide them special properties. Non-access modifiers are of 5 types,
1)Final 2)Static 3)Transient 4)Synchronized 5)Volatile #what is constructor in java? Constructor in java is a special type of method that is used to initialize the object.Java constructor is invoked at the time of object creation. It constructs the values i.e. provides 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 constructor.
- Constructor name must be same as its class name
- Constructor must have no explicit return type
#Types of java constructors:
There are two types of constructors:
1)Default constructor (no-arg constructor)
2)Parameterized constructor
#Java Default Constructor:
A constructor that have no parameter is known as default constructor.
#Example of default constructor:
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked 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 parameters 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();
} }