Вы находитесь на странице: 1из 52

Regularly used keywords in java

• Regularly used keywords are :


– Static
– New
– This
– Super
– final
• New:
• The new keyword dynamically allocates memory for an object.
• Syntax:
• class_name object _name = new class_name();
• EX.
• Box b1 = new Box(); Box b2 = new Box();
• 1
Static Keyword
• The static keyword is used in java mainly for memory management.
• Static keyword applied with Variables, methods and blocks. Static keyoword
belongs to the class than instance of the class..
• The static variable can be used to refer the common property of all object(it is
not unique for each object).
• Static variable gets memory only once in class area at the time of class loading.
• Advantage of Static variable
• It makes your program memory efficient (ie., it saves memory)

2
Static Example
class Student
{
void display()
int rollno; {
String name; System.out.println(rollno+" "+name+" "+college);

static String college="ITS"; }


public static void main(String args[])
{
Student(int r, String n) Student s1 = new Student(111,"Abinav");
{ Student s2 = new Student(222,"Harshitha");

rollno = r; s1.display();
s2.display();
name = n; }
}
}

3
Static Method Example

• A static method belongs to the class


rather than object of a class.
• A static method can be invoked without
the need for creating an instance of a
class.
• Static method can access static data
member and can change the value of it.

4
Static Method Example
class Student1
{
void display()
int rollno; {
String name; System.out.println(rollno+" "+name+" "+college);
static String college=“SAI ADITYA"; }
public static void main(String args[])
static void change() {
{ Student1.change();
college = “ADITYA"; Student1 s1 = new Student1(111,"Abinav");
Student1 s2 = new Student1(222,"Harshitha");
} Student1 s3 = new Student1(333,"Aadharsh");

Student1(int r, String n) s1.display();


{ s2.display();
rollno = r; s3.display();
name = n; }
}
}
5
Interview questions

• What is static variable?


– static variable is used to refer the common property of all objects (that is not unique for each
object) e.g. company name of employees,college name of students etc.

– .
static variable gets memory only once in class area at the time of class loading

• What is static method?


– A static method belongs to the class rather than object of a class.
– A static method can be invoked without the need for creating an
instance of a class.
– static method can access static data member and can change the
value of it.

6
Interview questions
• Why main method is static?
– Because object is not required to call static method if It were
non-static method,jvm creats object first then call main() method
that will lead to the problem of extra memory allocation.
• What is static block?
– Is used to initialize the static data member.
– It is excuted before main method at the time of classloading.
• Can we execute a program without main() method?
– Yes, one of the way is static block
• What if the static modifier is removed from the signature
of the main method?
– Program compiles. But at runtime throws an error "NoSuchMethodError".

7
this Keywords
• In java, this is areference variable that refers to the current object.
• Usage of java this keyword (6 usage of java this keyword)
– this keyword can be used to refer current class instance variable.
– this() can be used to invoke current class constructor.
– this keyword can be used to invoke current class method (implicitly)
– this can be passed as an argument in the method call.
– this can be passed as argument in the constructor call.
– this keyword can also be used to return the current class instance.

8
Without using this keyword

class student2
{
int id;
String name; public static void main(String args[])
{
student2(int id, String name) student2 s1 = new student2(111, "raaki");
{ student2 s2 = new student2(444, "Arvind");
id = id; s1.display();
name = name; s2.display();
} }
void display() }
{
System.out.println(id+" "+name);
}

9
With using this Keyword

class student12
{
int id;
String name; public static void main(String args[])
{
student2(int id, String name) student12 s1 = new student12(111, "raaki");
{ student12 s2 = new student12(444, "Arvind");
this.id = id; s1.display();
this.name = name; s2.display();
} }
void display() }
{
System.out.println(id+" "+name);
}

10
Method 2

• this() can be used to invoked current class constructor.

class Student13{
int id; void display(){System.out.println(id+" "
String name; +name);}
Student13(){System.out.println("defau
lt constructor is invoked");} public static void main(String args[])
{
Student13 e1 = new Student13(111,"kiran");
Student13(int id,String name){
Student13 e2 = new Student13(222,"Aryan");
this ();//it is used to invoked current cl e1.display();
ass constructor. e2.display();
this.id = id; }
this.name = name; }
}

11
Method 3
• The this keyword can be used to invoke current class method (implicitly)

void p(){
class S{
n();//complier will add this to invoke n()
void m(){
method as this.n()
System.out.println("method is invoked"
}
);
public static void main(String args[]){
}
void n(){
S s1 = new S();
this.m();//no need because compiler d
s1.p();
oes it for you.
}
}
}

12
Method 4
• this keyword can be passed as an argument in the method

class S2{
void m(S2 obj){
public static void main(String args[]){
System.out.println("method is invoked"
S2 s1 = new S2();
);
s1.p();
}
}
void p(){
}
m(this);
}

13
Method 5
• The this keyword can be passed as argument in the constructor call.

class A4{
class B{
int data=10;
A4 obj;
A4(){
B(A4 obj){
B b=new B(this);
this.obj=obj;
b.display();
}
}
void display(){
public static void main(String args[]){
System.out.println(obj.data);//using d
ata member of A4 class
A4 a=new A4();
}
}
}
}

14
Method 6
• The this keyword can be used to return current class instance

class A{
A getA(){ class Test1{
return this; public static void main(String args[]){
} new A().getA().msg();
void msg(){System.out.println("Hello jav }
a");} }
}

15
UsingThis Keyword Example
class Rectangle{
int length,breadth;
void show(int length,int breadth){
this.length=length;
this.breadth=breadth;
}
int calculate(){
return(length*breadth);
}
}
class UseOfThisOperator{
public static void main(String[] args){
Rectangle rectangle=new Rectangle();
rectangle.show(5,6);
int area = rectangle.calculate();
System.out.println("The area of a Rectangle is : " + area);
}
}
16
Inheritance
• Inheritance is a mechanism in which one object acquires all the properties
and behavior of another object.
• Inheritance represents the IS-A relationship, also known as parent-
child relationship.
• Why use inheritance in java
– For Method Overriding (so runtime polymorphism can be achieved).
– For Code Reusability.
• Syntax of Java Inheritance
• class Subclass-name extends Superclass-name
• {
• //methods and fields
• }
• The extends keyword indicates that you are making a new class that derives from
an existing class.
• In the terminology of Java, a class that is inherited is called a super class. The new
class is called a subclass.
17
Types of Inheritance
• Three types of inheritance in java: single, multilevel and hierarchical.

• Note: multiple and hybrid inheritance is supported through interface

18
Single Inheritance
class Employee
{
int salary=40000;
}

class Programmer extends Employee


{
int bonus = 10000;

public static void main(String args[])


{
Programmer p = new Programmer();
System.out.println("Programmer Salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}

19
Multilevel Inheritance
Class Z extends Y
{
Class X
public void methodZ()
{
{
public void methodX()
System.out.println("class Z
{
method");
System.out.println("Class X method");
}
}
public static void main(String args[])
}
{
Class Y extends X
Z obj = new Z();
{
obj.methodX(); //calling grand
public void methodY()
parent class method
{
obj.methodY(); //calling parent class
System.out.println("class Y method");
method
}
obj.methodZ(); //calling local method
}
}
}
20
Hierarchical Inheritance
Class D extends A
Class A {
{ public void methodD()
public void methodA() {
{ System.out.println("method of Class D");
System.out.println("method of Class A"); }
} }
} Class MyClass
Class B extends A {
{ public void methodB()
public void methodB() {
{ System.out.println("method of Class B");
System.out.println("method of Class B"); }
} public static void main(String args[])
} {
Class C extends A B obj1 = new B();
{ C obj2 = new C();
public void methodC() D obj3 = new D();
{ obj1.methodA();
System.out.println("method of Class C"); obj2.methodA();
} obj3.methodA();
} }
}
21
Method Overriding in Java

• Having the same name in the subclass as


declared in the parent class is known as
method overriding.
• If a subclass provides a specific
implementation of a method that is already
provided by its super class, it is know as
Method Overriding.

22
Advantage of Method Overriding

• Method overriding is used to provide specific


implementation of a method of a method that is
already provided by its super class.
• Method overriding is used for Runtime
Polymorphism.
• Rules for Method Overriding:
– Method must have same name as in the parent class.
– Method must have same parameter as in parent
class.

23
Example Without Overriding
//Problem without method overriding

class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}

class Bike extends Vehicle


{
public static void main(String args[])
{
Bike obj = new Bike();
obj.run();
}
}
24
Example for Method Overriding
class Bike1 extends Vehicle
//Problem with method {
overriding void run()
{
class Vehicle System.out.println("Bike is
{ running Safely");
}
void run() public static void main(String
{ args[])
System.out.println("Vehicle {
is running"); Bike1 obj = new Bike1();
} obj.run();
Can we override Static Method?
}
} }
No, we cannot. It can be done
only by runtime Polymorphism.

25
Super Keyword

• Super is a reference variable that is used


to refer immediate parent class object.
• Uses of super keyword
– super is used to refer immediate parent class
instance variable.
– super() is used to invoke immediate parent
class constructor.
– Super is used to invoke immediate parent
class method.
26
Super Example

• Problem without super keyword


class Vehicle
{
int speed=50;

}
class Bike2 extends Vehicle
{
int speed = 100;
void display()
{
System.out.println(speed);
}
public static void main(String args[])
{
Bike2 b=new Bike2();
b.display();
}
}

27
Using Super Keyword
class Vehicle
{
int speed=50;

}
class Bike3 extends Vehicle
{
int speed = 100;
void display()
{
System.out.println(super.speed);
}
public static void main(String args[])
{
Bike3 b=new Bike3();
b.display();
}
}

28
Method 2
• super keyword is used to invoke the parent class constructor.

class Vehicle class Bike5 extends Vehicle


{
{ Bike5()
Vehicle() {
{ super();
System.out.println("Bike is created");
System.out.println("Vehicle }
is created"); public static void main(String args[])
} {
Bike5 b=new Bike5();
}
} }
29
Method 3

• super can be used to invoke parent class method

void display(){
message();//will invoke current class me
class Person{ ssage() method
void message(){System.out.println("welc super.message();//will invoke parent cla
ome");} ss message() method
} }

class Student16 extends Person{ public static void main(String args[]){


void message(){System.out.println("welc Student16 s=new Student16();
ome to java");} s.display();
}
}

30
Final Keyword

• The final keyword in java is used to restrict the user.


• The final keyword can be used in many context. Final
can be:
– variable,
– method
– Class
• The final keyword can be applied with the variables, that
have no value it is called blank final variable.
• It can be initialized in the constructor only.
• The blank final variable can be static also which will be
initialized in the static block only.
31
Final Variable

• If you make any variable as final, you cannot change the


value of the final variable(it will be constant).
class Bike5
public static void main(String
{
args[])
final int speedlimit=90;
{
Bike5 obj=new Bike5();
void run() obj.run();
{ }
speedlimit=400; } Output: Compiler -
} error

32
Final method

• If you make any method as final you cannot override it.


class suziki extends Bike6
{
class Bike6 void run()
{ {
System.out.println("running
final void run() safetly");
{ }

public static void main(String args[])


System.out.println("running"); {
} suziki obj1=new suziki();
} suziki.run();
} Output: Compiler
} -error
33
Final Class

• If you make any class as final, you cannot extend it.


final class Bike7
{}

class Honda extends Bike7


{
void run()
{
System.out.println("running safetly");
}
public static void main(String args[])
{
Honda honda = new Honda();
honda.run();
}
}
34
Interview Questions

• Is final method inherited?


– Yes, final method is inherited but you cannot override it.

class Bike{
final void run(){System.out.println("running...");}
}
class Honda2 extends Bike{
public static void main(String args[]){
new Honda2().run();
}
}

35
Polymorphism
• An object of a sub class can be
used whenever its super class
object is required . This is known as
polymorphism.
• In simple terms polymorphism
means that a variable of super type
can refer to a sub type object.
• There are two types of
polymorphism :
– Compile-time polymorphism
– Runtime Polymorphism

36
Polymorphism

• Polymorphism allows one interface to be used for


a set of actions i.e. one name may refer to
different functionality.
• Polymorphism allows a object to accept different
requests of a client and responds according to the
current state of the runtime system, all without
bothering the user.

37
Compile-time Polymorphism

• In compile-time Polymorphism, method to


be invoked is determined at the compile
time. Compile time polymorphism is
supported through the method
overloading concept in java.
• Method overloading means having
multiple methods with same name but with
different signature (number, type and
order of parameters).
38
Compile-time Polymorphism

class A{ public class polyone{


public void fun1(int x){ public static void
System.out.println("The value main(String[] args){
of class A is : " + x); A obj=new A();
}
public void fun1(int x,int y){
System.out.println("The value obj.fun1(2);
of class B is : " + x + " and " +
y); obj.fun1(2,3);
} }
} }
39
Runtime Polymorphism

• In rumtime polymorphism, the method to


be invoked is determined at the run time.
The example of run time polymorphism
is method overriding. When a subclass
contains a method with the same name
and signature as in the super class then it
is called as method overriding.

40
Runtime Polymorphism
public class polytwo{
class A{
public static void main(String[]
public void fun1(int x){
args){
System.out.println("int in Class A is
A obj;
: "+ x);
}
obj= new A(); // line 1
}
obj.fun1(2); // line 2 (prints "int
in Class A is : 2")
class B extends A{
public void fun1(int x){
obj=new B(); // line 3
System.out.println("int in Class B is
obj.fun1(5); // line 4 (prints ""int
: "+ x);
in Class B is : 5")
}
}
}
}

41
Data Binding

• Connecting a method, call to a method body is called binding.

• It consist of two types:


– Static binding
– dynamic binding
• Variables have a type
• Ex: int data = 30;
• Here data variable is a type of int.

42
Data Binding
• References have a type

class Dog{
public static void main(String args[]){
Dog d1;//Here d1 is a type of Dog
}
}

• Objects have a type

class Animal{}

class Dog extends Animal{


public static void main(String args[]){
Dog d1=new Dog();
}
} 43
Static Binding
• When type of the object is determined at compiled time(by the compiler), it
is called as static binding.
• If there is any private, final or static method in class, it is a static binding.
class Dog
{
private void eat()
{
System.out.println("dog is eating...");
}

public static void main(String args[])


{
Dog d1 = new Dog();
d1.eat();
}
}
44
Dynamic Binding
• When type of the object is determined at run-time , it is known as
dynamic binding.
class Dog1 extends Animal
class Animal {
{ void eat()
{
void eat() System.out.println("dog is
{ eating...");
System.out.println("animal is }
eating..."); public static void main(String args[])
} {
} Animal a = new Dog1();
a.eat();
}
}
45
Interview Questions

• What is this in java?


– It is a keyword that that refers to the current object.

• What is Inheritance?
– Inheritance is a mechanism in which one object acquires all the
properties and behaviour of another object of another class. It
represents IS-A relationship. It is used for Code Resusability and
Method Overriding.
• Why multiple inheritance is not supported in java?
– To reduce the complexity and simplify the language, multiple
inheritance is not supported in java in case of class.

46
Interview Questions

• What is super in java?


– It is a keyword that refers to the immediate parent class object.
• Can you use this() and super() both in a constructor?
– No. Because super() or this() must be the first statement.
• What is object cloning?
– The object cloning is used to create the exact copy of an object.
• What is method overloading?
– If a class have multiple methods by same name but different
parameters, it is known as Method Overloading. It increases the
readability of the program

47
Interview Questions

• Why method overloading is not possible by changing the


return type in java?
– Because of ambiguity
• Can we overload main() method?
– Yes, You can have many main() methods in a class by
overloading the main method.

• What is method overriding:


• If a subclass provides a specific implementation of a method that is
already provided by its parent class, it is known as Method
Overriding. It is used for runtime polymorphism and to provide the
specific implementation of the method.

48
Interview Questions

• Can we override static method?


– No, you can't override the static method because they are the
part of class not object.
• Why we cannot override static method?
– It is because the static method is the part of class and it is bound
with class whereas instance method is bound with object and
static gets memory in class area and instance gets memory in
heap.
• Can we override the overloaded method?
– Yes.

49
Interview Questions

• Difference between method Overloading


and Overriding?
Method Overloading Method Overriding
1) Method overloading increases Method overriding provides the
the readability of the program. specific implementation of the
method that is already provided
by its super class.
2) method overlaoding is occurs Method overriding occurs in two
within the class. classes that have IS-A
relationship.
3) In this case, parameter must In this case, parameter must be
be different. same.

50
Interview Questions

• What is final variable?


– If you make any variable as final, you cannot change the value of
final variable(It will be constant).
• What is final method?
– Final methods can't be overriden.
• What is final class?
– Final class can't be inherited.
• Can you declare the main method as final?
– Yes, such as, public static final void main(String[] args){}.

51
Interview Question

• What is Runtime Polymorphism?


– Runtime polymorphism or dynamic method dispatch is a process
in which a call to an overridden method is resolved at runtime
rather than at compile-time.
– An overridden method is called through the reference variable of
a super class.

• What is the difference between static


binding and dynamic binding?
– Static binding type of object is determined at compile time
whereas in dynamic binding type of object is determined at
runtime.

52

Вам также может понравиться