Day 5
DAY 5
JAVA
ACCESS SPECIFIERS:
The access to classes, constructors, methods and fields are regulated using access modifiers i.e. a class can control what information or data can be accessible by other classes. To take advantage of encapsulation, you should minimize access whenever possible.
Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes. A member has package or default accessibility when no accessibility modifier is specified.
Access Modifiers
1. private
2. protected
3. default
4. public
2. protected
3. default
4. public
public access modifier
Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.
private access modifier
The private (most restrictive) fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods or constructors declared private are strictly controlled, which means they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.
protected access modifier
The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.
default access modifier
Java provides a default specifier which is used when no access modifier is present. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package. The default modifier is not used for fields and methods within an interface.
FINAL keyword
In the Java programming language, the
final
keyword is used in several contexts to define an entity that can only be assigned once.
Once a
final
variable has been assigned, it always contains the same value. If a final
variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object (this property offinal
is called non-transitivity). This applies also to arrays, because arrays are objects; if a final
variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.
final i=10;
SUPER keyword
The super keyword in java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
Usage of java super Keyword
- super can be used to refer immediate parent class instance variable.
- super can be used to invoke immediate parent class method.
- super() can be used to invoke immediate parent class constructor.
class Person{
int id;
String name;
Person(int id,String name){
this.id=id;
this.name=name;
}
}
class Emp extends Person{
float salary;
Emp(int id,String name,float salary){
super(id,name);//reusing parent constructor
this.salary=salary;
}
void display(){System.out.println(id+" "+name+" "+salary);}
}
class TestSuper5{
public static void main(String[] args){
Emp e1=new Emp(1,"ankit",45000f);
e1.display();
}}
METHOD OVERLOADING vs METHOD OVERRIDING
Practice work:
1. Write a program to find the greatest no. between three numbers.
package july16;
import java.util.Scanner;
public class greater_3 {
public static void main(String s[])
{
System.out.println("enter first no.:");
Scanner obj=new Scanner(System.in);
int a=obj.nextInt();
System.out.println("enter second no.:");
int b=obj.nextInt();
System.out.println("enter third no.:");
int c=obj.nextInt();
if( a>b && a>c)
{
System.out.println("a is greater");
}
else if( b>a && b>c )
{
System.out.println("b is greater");
}
else
{
System.out.println("c is greater");
}
}
}
2. Write a program to find sum of two numbers by taking input from users.
package july16;
import java.util.Scanner;
public class addition {
public static void main(String s[])
{
System.out.println("enter first no.:");
Scanner obj=new Scanner(System.in);
int a=obj.nextInt();
System.out.println("enter second no.:");
int b=obj.nextInt();
int c=a+b;
System.out.println("sum:"+c);
}
}
3. Write a program to swap two numbers using third variable.
package july16;
public class swapping
{
public static void main(String s[])
{
int a=10;
int b=20;
int c=0;
System.out.println("a before"+a);
System.out.println("b before"+b);
c=a;
a=b;
b=c;
System.out.println("a after"+a);
System.out.println("b after"+b);
}
}
4. Write a program to swap two numbers without using third variable.
package july16;
public class swap_without_third
{
public static void main(String s[])
{
int a=10;
int b=20;
System.out.println("a before"+a);
System.out.println("b before"+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("a after"+a);
System.out.println("b after"+b);
}
}
Comments
Post a Comment