0% found this document useful (0 votes)
5 views

1st UNIT.pdf

The document covers various fundamental concepts in Java, including constructors, data types, OOP principles, and the use of the 'this' keyword with examples. It also discusses Java features and visibility modifiers. Each section provides explanations and example programs to illustrate the concepts effectively.

Uploaded by

charankumar1531
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

1st UNIT.pdf

The document covers various fundamental concepts in Java, including constructors, data types, OOP principles, and the use of the 'this' keyword with examples. It also discusses Java features and visibility modifiers. Each section provides explanations and example programs to illustrate the concepts effectively.

Uploaded by

charankumar1531
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1st UNIT

1. llustrate constructors and write various types of constructors with an example program.
Solution:
2
4
5
2. Discuss Data Type in java with example program.
Solution:

6
7
8
3. Outline OOPS concepts.
Solution:

9
10
4. Explain various uses of “this” keyword with an example program.
Solution:

11
1) this: to refer current class instance variable
The this keyword can be used to refer current class instance variable. If there is ambiguity
between the instance variables and parameters, this keyword resolves the problem of
ambiguity.

class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{

public static void main(String args[]){


Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display(); s2.display(); }}

2) this: to invoke current class method


You may invoke the method of the current class by using the this keyword. If you don't use
the this keyword, compiler automatically adds this keyword while invoking the method. Let's
see the example

12
3) this: to pass as an argument in the method
The this keyword can also be passed as an argument in the method. It is mainly used in the
event handling. Let's see the example:

More uses of this keyword is :

-> this keyword can be used to return current class instance


We can return this keyword as an statement from the method. In such case, return type of the
method must be the class type (non-primitive). -> The this() constructor call should be used to
reuse the constructor from the constructor. It
maintains the chain between the constructors i.e. it is used for constructor chaining
-> to invoke current class constructor
The this() constructor call can be used to invoke the current class constructor. It is used to
reuse the constructor. In other words, it is used for constructor chaining.

14
5. Java Features / Java Buzz words.
Solution:
6. Explain the different Visibility modifiers in Java. / Explain the different access modifiers in
Java.
Solution :

17
20

You might also like