Академический Документы
Профессиональный Документы
Культура Документы
2
Static Example
class Student
{
void display()
int rollno; {
String name; System.out.println(rollno+" "+name+" "+college);
rollno = r; s1.display();
s2.display();
name = n; }
}
}
3
Static Method Example
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");
– .
static variable gets memory only once in class area at the time of class loading
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
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.
18
Single Inheritance
class Employee
{
int salary=40000;
}
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
22
Advantage of Method Overriding
23
Example Without Overriding
//Problem without method overriding
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
25
Super Keyword
}
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.
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
} }
30
Final Keyword
32
Final method
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
37
Compile-time Polymorphism
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
42
Data Binding
• References have a type
class Dog{
public static void main(String args[]){
Dog d1;//Here d1 is a type of Dog
}
}
class Animal{}
• 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
47
Interview Questions
48
Interview Questions
49
Interview Questions
50
Interview Questions
51
Interview Question
52