~~ CORE-JAVA ~~
1. INTORDUCTION
1.1 Java Intorduction
1.2 Java history
1.3 Java features
1.4 Diff b/w Java & C++
1.5 Java Variables
1.6 Java Data Types
1.7 Typecasting
1.8 Arrays
2. OOPS
2.1 Introduction
2.2 Class
2.3 Object
2.4 Static Keywords
2.5 Constructors
2.6 This Key Word
3. INHERITANCE
3.1 Inheritance (IS-A)
3.2 Aggregation (HAS-A)
4. POLYMORPHISM
4.1 Polymorphism & Its Type
4.2 Method Overloading
4.3 Method Overriding
4.4 Super Keyword
4.5 Final Keyword
4.6 Dynamic Binding
5. ABSTRACTION
5.1 Abstract Classes & Methods
5.2 Interfaces
5.3 Abstract vs Interface
6. ENCAPSULATION
6.1 Encapsulation
7. STRING MANIPULATIONS
7.1 String
7.2 String Buffer
7.3 String Tokenizer
8. PACKAGES
8.1 Predefined packages
8.2 Userdefined Packages
8.3 Access Specifiers
9. EXCEPTION HANDLING
9.1 Introduction
9.2 Pre Defined Exceptions
9.3 Try-Catch-Finally
9.4 Throws, throw
9.5 User Defined Exception
10. MULTITHREADING
10.1 Introduction
10.2 Thread Creations
10.3 Thread Life Cycle
10.4 Life Cycle Methods
10.5 Synchronization
10.6 Wait() notify() notify all()
11. WRAPPERCLASSES
11.1 Introduction
11.2 Wrapper Classes
12. COLLECTION FRAME WORK
12.1 Introduction
12.2 List interface
12.3 Set interface 12.4 Map interface
13. INNER CLASSES
13.1 Introduction
13.2 Member inner class
13.3 Static inner class
13.4 Local inner class
13.5 Anonymous inner class
14. CoreJava Interview Questions
Java | Constructor :
- In Java we know, we are creating Object but only Object creation is not important compulsory we should perform initialization then only that Object will provide proper response.
- Whenever we are creating Object, in backend some piece of code will execute automatically to perform initialization. This peace of code is nothing but Constructor only.
- So here we can say the main objective of Constructor is to initialize the Object.
- At the time of Object creation if we want to perform an initialization of instance variable then we should go for Constructor. Other than initialization if we want to perform any activity at the time of Object creation then we should go for instance block.
Rules To Create Constructor :
- The name of the Class and name of the Constructor should be same.
- Return type should not be there for Constructor (void also should not be there.)
- For Constructor only below modifiers are possible :
- private
- public
- protected
- default
Note :
- If we will use any other modifier, immediately we will get Compile Time Error.
- If we are using any return type for Constructor, then compiler will not throw any compile or run time error. But it will be treated as method.
Types of Constructor :
- Default Constructor
- Parameterized Constructor
1. Default Constructor :
When a Constructor doesn’t have any parameter, then it will be Default Constructor. Whenever we will create Object by-default this constructor will execute. Below is simple example of Default Constructor.
Example :
public class Book {
//Creating Default Constructor
Book() {
System.out.println(“Book object is Created.”);
}
public static void main(String[] args) {
//Creating Object or Calling Constructor
Book b = new Book();
}
}
OutPut :
Book object is Created.
Note : If there is no constructor in a Class, then compiler automatically internal it will create
2. Parameterized Constructor :
When a constructor have some parameter, then it will be Parameterized Constructor. We are creating Parameterized constructor to initialize the instance variable. Below is simple example of Parameterized Constructor.
Example :
public class Book {
int id;
String name;
//Parameterized Constructor
Book(int i, String n) {
id = i;
name = n;
}
//Method to display Book Value
void display() {
System.out.println(“BookId is : ” + id + ” and Book Name is : ” + name);
}
public static void main(String[] args) {
//Creating Object or Calling Constructor
Book b = new Book(100, “Java”);
//Calling Method to display Value
b.display();
}
}
Output :
BookId is : 100 and Book Name is : Java
Constructor Overloading :
In Java, when we have such type of Class where multiple constructors are there with different parameter list. Then it’s nothing but Constructor Overloading. In Coming topic we will see Overloading in detail. Below is simple example of Cosntructor Overloading.
Example :
public class Book {
int id;
String name;
String description;
//Two argument parameterized Constructor
Book(int i, String n) {
id = i;
name = n;
}
//Three argument parameterized Constructor
Book(int i, String n, String d) {
id = i;
name = n;
description = d;
}
//Method to display Book Value
void display() {
System.out.println(“BookId is : ” + id + ” ,Book Name is : ” + name + ” ,And BookDescription is : ” + description);
}
public static void main(String[] args) {
//Creating Object or Calling Two Argument Constructor
Book b1 = new Book(100, “Java”);
//Creating Object or Calling Three Argument Constructor
Book b2 = new Book(200, “Tibco”, “Description Of Tibco”);
//Calling Method to display Value
b1.display();
b2.display();
}
}
Output :
BookId is : 100 ,Book Name is : Java ,And BookDescription is : null BookId is : 200 ,Book Name is : Tibco ,And BookDescription is : Description Of Tibco
Note :