Important concept of java part-1
Important points of java
---------------------------
Save the Program
-------------------
After writing the application must save the application by using (.java) extension.
---- While saving the application must fallow two rules
---------------------------
1. If the source file contains public class then public class name & Source filename must be same (publicClassName.java). Otherwise compiler generate error message.
2.if the source file does not contain public class then save the source file with any name (anyName.java) like A.java ,Atul.java ....
Note: - The source file allowed only one public class, if we are trying to declare multiple public classes then compiler generate error message.
Compilation process
-----------------------
Compile the java application by using javac command.
Syntax:- Javac filename
Javac Test.java
Whenever we are performing compilation the compiler will check the syntax errors.
1. If the application contains syntax errors then compiler will generate error message in the form of compilation error.
2. If the application does not contains syntax errors then compiler will generate .class files.(conversion of .java to .class)
Note: - in java .class files generated by compiler at compilation time and .class file generation based on number of classes present in source file.
----If the source file contains 100 classes after compilation compiler generates 100
We can compile multiple file at same time like this
-----------------
javac B.java C.java
Execution process
-----------------------
5:- Execution process.
Run /execute the java application by using java command.
Syntax:-
Java class-name
example --Java Test
Whenever you are executing particular class file then JVM perform fallowing actions.
1. JVM wills loads corresponding .class file byte code into memory.
2. After loading .class file JVM calls main method to start the execution process.
In above two cases if the class file or main method is not available then at runtime JVM will generate error message.
If the main method is not available: “Main method not found in class A, please define the main method”.
If the .class is not available : “Could not find main class”.
Important points
-------------------
1.Java contains 14 predefined packages but the default package in java is java.lang
2.The class contains main method is called Main class and java allows to declare multiple main class in a single source file.
3.The source file is allows to declare only one public class, if you are declaring morethan one public class compiler generate error message.
Naming Convention
-------------------------
Classes:-
1.Class name start with upper case letter and every inner word starts with upper case letter.
2.This convention is also known as camel case convention.
Example:-String StringBuffer
Interfaces :-
1. Interface name starts with upper case and every inner word starts with upper case letter.
2. This convention is also known as camel case convention.
Ex: Serializable Cloneable RandomAccess
Methods :-
1- Method name starts with lower case letter and every inner word starts with upper case letter.
2- This convention is also known as mixed case convention
Ex:- post() charAt()
Variables:
------------
1.Variable name starts with lower case letter and every inner word starts with upper case letter
Example:- userName password
Package :-
1.Package name is always must written in lower case letters.
Example:-
java.lang
Constants:-
1.While declaring constants all the words are uppercase letters .
Example: MAX_PRIORITY
Class Elements:-
------------------
variables
methods
constructors
instance blocks
static blocks static
Variables
----------
Variables are also known as fields of a class or properties of a class.
There are three types of variables in java
1. Local variables.
2. Instance variables.
3. Static variables.
Local variables:-
-------------------
1.The variables which are declare inside a method or constructor or blocks those variables are called local variables.
2.It is possible to access local variables only inside the method or constructor or blocks only, it is not possible to access outside of method or constructor or blocks.
3.For the local variables memory allocated when method starts and memory released when method completed.
4.In java before using local variables must initialize some values to the variables otherwise compiler will raise compilation error “variable a might not have been initialized”.
Areas of java language
-------------------
There are two types areas in java.
1) Instance Area.
2) Static Area.
Instance Area:-
void m1() //instance method
{
Logics here //instance area
}
Static Area:-
----------
Static void m1() //static method
{ Logics here //static area
}
Instance variables (non-static variables):-
-------------------------------
1.The variables which are declare inside a class but outside of methods those variables are called instance variables.
2.For the instance variables memory allocated during object creation & memory released when object is destroyed.
3.for the instance variables JVM will assign default values.
4.Instance variable can direclty access in Instance area.
5.In static area Instance variable can access by using object.
Static variables
--------------------
1.The variables which are declared inside the class but outside of the methods with static modifier those variables are called static variables.
2.Static variables memory allocated during .class file loading and memory released at .class file unloading time.
3.for the static variables JVM will assign default values.
4.By Using class Name we can access Static variable in Instance area and also in static area....
Static variables calling: - We are able to access the static members inside the static area in three ways.
1. Direct accessing.
2. By using class name.
3. By using reference variable.
Methods
-------------
Method Signature:-
-----------------
Method-name & parameters list is called method signature.
Syntax:- Method-name(parameter-list)
Example
--------
void m1(int a)
void m2(int a,int b)
1. Instance methods are bounded with objects hence call the instance methods by using object name(reference variable).
2. Static methods are bounded with class hence access the static methods by using class-name.
Note:-
---------
This keyword required:-
When intstance & local variables having same name, then to represent instance variables use this keyword.
2. this keyword representing current class objects.
Constructor
---------------
1) Constructors are used to write block of java code that code will be executed during object creation.
2) Constructors are used to initialize instance variables during object creation.
Default Constructor
---------------------
1. Inside the class if we are not declaring at least one constructor then compiler generates zero argument constructors with empty implementation at the time of compilation.
2.The compiler generated constructor is called default constructor.
User defined constructor:-
----------------------------
The constructors which are declared by user are called user defined constructor.
Points
-------
1.Inside the class if we are declaring at least one constructor (either 0-arg or parameterized) then compiler won’t generate default constructor.
Note :- default constructor is zero argument constructor but all zero argument constructors are not default constructors
Object creation formats:-
-----------------------------
2-formats of object creation.
1) Named object (having reference variable) Test t = new Test();
2) Nameless object (without reference variable) new Test();
Constructor calling:-
------------------------
1.To call Current class constructor use this keyword
this(); ----> current class 0-arg constructor calling
this(10); ----> current class 1-arg constructor calling
Object creation parts:- Every object creation having three parts.
1) Declaration:-
Test t; //t is Test type
Student s; //s is Student type
2) Instantiation:-(just object creation)
new Test(); //Test object
3) initialization:-(during object creation perform initialization)
new Test(10,20); //during object creation 10,20 values initialized
Instance Blocks:-
--------------------
1.Instance blocks are used to write the logics of projects & these logics are executed during object creation just before constructor execution.
Syntax
---------
Instance block syntax
{
//logics here
}
static block:-
----------------
1.Static blocks are used to write the logics of project that logics are executed during .class file loading time.
Static block syntax
------------------
static
{ //logics here
}
Scanner class
------------
1.Scanner class present in java.util package and it is introduced in 1.5 versions & it is used to take dynamic input from the keyboard.
2. To communicate with system resources use System class & to take input from keyboard use in variable
Scanner s = new Scanner(System.in); //Scanner object creation
to get int value ----> s.nextInt()
to get float value ----> s.nextFloat()
to get byte value ----> s.nextbyte()
to get String value ----> s.next()
to get single line ----> s.nextLine()
Classess and Objects
----------------
Class is a logical entity it contains logics whereas object is physical entity it is representing memory.
1. Class is blue print it decides object creation without class we are unable to create object.
2. Based on single class (blue print) it is possible to create multiple objects but every object occupies memory.
3. We are declaring the class by using class keyword but we are creating object by using new keyword.
Objects
----------
Every object contains three characteristics,
1) State : well defined condition of an item (instance variable/fields/properties)
2) Behavior : effects on an item (methods/behavior)
3) identity : identification number of an item(hash code)
The main building blocks of oops are
1. Inheritance
2. Polymorphism
3. Abstraction
4. Encapsulation
Inheritance:-
-------------
1.The process of acquiring properties (variables) & methods (behaviors) from one class to another class is called inheritance
2.We are achieving inheritance concept by using extends keyword. Inheritance is also known as is-a relationship
3.In java if we are extending the class then it will be parent class , if we are not extending the class then Object class will become parent class.
Single inheritance:-
1. One class has only one direct super class is called single inheritance.
Multilevel inheritance:-
1.One Sub class is extending Parent class then that sub class will become Parent class of next extended class this flow is called multilevel inheritance.
Multiple inheritance:-
-------------------------
1. One sub class is extending more than one super class is called Multiple inheritance and java not supporting multiple inheritance because it is creating ambiguity problems (confusion state) .
2. Java not supporting multiple inheritances hence in java one class able to extends only one class at a time but it is not possible to extends more than one class.
Super keyword:-
------------------
“this” keyword is used to represent current class object& “super” keyword is used to represent super class object.
1. Super class variables.
2. Super class methods.
3. Super class constructors.
4. Super class instance blocks.
5. Super class static blocks.
Polymorphism:-
----------------
1.The ability to appear in more forms is called polymorphism
There are two types of polymorphism in java,
1) Compile time polymorphism / static binding / early binding
Example :- method overloading.
2) Runtime polymorphism /dynamic binding /late binding.
Example :- method overriding.
Compile time polymorphism [Method Overloading]:-
-------------------------------------------------------------
1) If java class allows more than one method with same name but different number of arguments or same number of arguments but different data types those methods are called overloaded methods.
a. Same method name but different number of arguments.
void m1(int a){ }
void m1(int a,int b){ }
b. Same method name & same number of arguments but different data types.
void m1(int a){ }
void m1(char ch){ }
2) To achieve overloading concept one java class sufficient.
3) It is possible to overload any number of methods in single java class.
Runtime polymorphism [Method Overriding]:-
----------------------------
1.To achieve method overloading one java class sufficient but to achieve method overriding we required two java classes with parent and child relationship.
2.
In overriding parent class method is called ===> overridden method
Child class method is called ===> overriding method
3.Overridden method signature & overriding method signatures must be same.
4.Final methods can’t override.
5.Static method can’t override.
Final modifier:- Final is the modifier applicable for classes, methods and variables
----------------
Preventing inheritance:-
1. You can prevent sub class creation by using final modifier.
2. If a parent class declared as final we can’t create sub class for that class.
1.if a class is declared as final, then we cannot inherit that class it means we cannot create child class for that final class.
Note-
-------
Every method present inside a final class is always final but every variable present inside the final class not be final variable.
2.If a method declared as a final we can not override that method in child class.
3.If a variable declared as a final we can not reassign that variable if we are trying to reassign compiler generate error message.
Abstraction:-
----------------
--The process highlighting the set of services and hiding the internal implementation is called abstraction
--We are achieving abstraction concept by using Abstract classes
There are two types of methods in java
a. Normal methods
b. Abstract methods
Normal methods:-
----------------
Normal method is a method which contains method declaration as well as method implementation.
Example:-
void m1() ---method declaration
{
body; ---method implementation
}
Abstract methods:-
---------------------
1) The method which is having only method declaration but not method implementations such type of methods are called abstract Methods.
2) In java every abstract method must end with “ ; ”.
Example : - abstract void m1 (); ---- method declaration
Based on above representation of methods the classes are divided into two types
1) Normal classes.
2) Abstract classes
Abstract class may contain abstract methods or may not contains abstract methods but object creation is not possible.
---Abstract modifier is applicable for methods and classes but not for variables.
---For the abstract classes object creation not possible, if you are trying to create object compiler will generate error message.
------Abstract class contains abstract methods for that abstract methods provide the implementation in child classes.
---if the child class is unable to provide the implementation of all parent class abstract methods at that situation declare that class with abstract modifier then take one more child class to complete the implementation of remaining abstract methods.
--- It is possible to declare multiple child classes but at final complete the implementation of all methods.
Post a Comment