1.Static Keyword:-
- In java we can set static as variables,methods,blocks.
- If you declare any variable as static, it is known static variable.
- The static keyword in java is used mainly for memory management.
- The static variable can be used to refer the common property of all objects .
we can discuss static keyword by example:-
class Employee
{
int empid;
String empname;
static String company="HCL";
Employee(int id,String n)
{
empid=id;
empname=n;
}
void display()
{
System.out.println("empid+""+empname""+company);
}
public static void main(String arg[])
{
Employee e1=new Employee(101,"Alex");
Employee e2=new Employee(102,"Mark");
e1.display();
e2.display();
}
}
Output:- 101 Alex HCL
102 Mark HCL
2.Super Keyword:-
- The super keyword is also be used to invoke parent class method.
- It should be used if subclass contains the same method as parent class.
- In other word it is used if method is overridden.
we can discuss static keyword by example:-
class Animal
{
void eat()
{
System.out.println("Eating.....");
}
class Dog extends Animal
{
void eat()
{
System.out.println("Eating Bread");
}
void bark()
{
System.out.println("Barking");
}
super.eat(); //use of super keyword
}
}
class TestSuper
{
public static void main(String arg[])
{
Dog d1=new Dog();
d1.bark();
}
}
Output:- Eating.....
3.Final Keyword:-
- Final keyword can be applied to the variable,method,classes
1.Final Variable:-
- The value of variable can be prevented from being modified by declaring a variable as final.
- Example:- int Max=100;
- A method can be prevented from being overridden by the subclass by declaring the method as final.
- Example:-
final void show() // can't overridden
3.Final Classes:-
- A class which can not be further inherited or can not have any subclass is known as final classes.
- To make a class final,the class declaration is preceded by the final keyword.
- If class declared as final,all of its methods are automatically declared as final.
- Example:-
{
System.out.println("Web Coders");
}
class B extends A //ERROR
{
System.out.println("Can not inherited");
}
That’s a nice site you people are carrying out there. interview
ReplyDeleteThank You Pheobe22
Delete