Dt : 14/7/2022
Ex:
Cars.java
package test;
public enum Cars {
figo(700),alto(500),dezire(600);//elements
public int price;
private Cars(int price){
this.price=price;
}
public int getPrice() {
return price;
}
DemoEnum.java(MainClass)
package maccess;
import test.Cars;
public class DemoEnum {
public static void main(String[] args) {
Cars c[] = Cars.values();//Generating Array Object
System.out.println("===Displat Enum===");
for(Cars k : c) {
System.out.println(k.toString()+" costs "+
k.getPrice()+" thousand dollars");
}
System.out.println("===Based on index===");
System.out.println(c[0].getPrice());
c[0].price = 13000;
System.out.println("===Based on index===");
System.out.println(c[0].getPrice());
}
}
o/p:
===Displat Enum===
figo costs 700 thousand dollars
alto costs 500 thousand dollars
dezire costs 600 thousand dollars
Diagram:
========================================================
Note:
=>Each element in enum is internally generated as Enum<E> object
=>The constructors which are declared within the enum are
automatically 'private constructor'.
=>In realtime Enum<E> is used when we have defined list of
collection.
(Enum<E> is less used when compared to Classes and Interfaces)
===========================================================
*imp
Summary of objects generated from CoreJava:(Container objects)
1.User defined Class objects
2.String Objects
3.WrapperClass Objects
4.Array Objects
5.Collection<E> Objects
6.Map<K,V> objects
7.Enum<E> Objects
List of objects by Sub-category:
1.User defined Class objects
2.String Objects
a.String Object
b.StringBuffer Object
c.StringBuilder Object
3.WrapperClass Objects
a.Byte Object
b.Short Object
c.Integer Object
d.Long Object
e.Float Object
f.Double Object
g.Character Object
h.Boolean Object
4.Array Objects
a.Array holding User defined Class Objects
b.Array holding String Objects
c.Array holding WrapperClass Objects
d.Object Array(Array holding Dis-Similer Objects)
e.Jagged Array(Array holding Array objects)
5.Collection<E> Objects
a.Set<E>
(i)HashSet<E> object
(ii)LinkedHashSet<E> object
(iii)TreeSet<E> object
b.List<E>
(i)ArrayList<E> object
(ii)LinkedList<E> object
(iii)Vector<E> object
=>Stack<E> object
c.Queue<E>
(i)PriorityQueue<E> object
=>Deque<E>
(ii)ArrayDeque<E> object
(iii)LinkedList<E> object
6.Map<K,V> objects
(a)HashMap<K,V> object
(b)LinkedHashMap<K,V> object
(c)TreeMap<K,V> object
(d)Hashtable Object
7.Enum<E> Objects
======================================================
List of Cursor Objects:
1.Iterator<E>
2.ListIterator<E>
3.Enumeration<E>
4.Spliterator<E>
====================================================
List of Utility Objects:
1.Scanner
2.StringTokenizer
3.StringJoiner
4.Collections
5.Arrays
===============================================
faq:
wt is the diff b/w
(i)Container Objects
(ii)Cursor Objects
(iii)Utility Objects
(i)Container Objects:
=>The Objects which contain data or which hold data are known
as Container objects.
(ii)Cursor Objects:
=>The objects which are used to retrieve the elements from the
objects are known as Cursor objects.
(iii)Utility Objects:
=>The Objects which perform operations on other objects are
known as Utility Objects.
======================================================
faq:
wt is the diff b/w
(i)Array
(ii)ArrayList<E>
=>Array size once defined cannot be modified at runtime and which
is not preferable to hold dynamic data.
=>ArrayList<E> is not size restricted and can hold unlimited data
to runtime.
==========================================================
Ex:(Program to demonstrate Array Sorting)
ArraySort.java
package maccess;
import java.util.ArrayList;
import java.util.Arrays;
public class ArraySort {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(new Integer(13));
al.add(new Integer(11));
al.add(new Integer(12));
al.add(new Integer(9));
Object o[] = al.toArray();//Convert into Array
Arrays.sort(o);//Sorting on Array
System.out.println("===Sorted Array===");
for(Object k : o) {
System.out.print(k.toString()+" ");
o/p:
===Sorted Array===
9 11 12 13
=========================================================
faq:
define Data Structure?
=>The process of organizing data in a proper order or Structure
is known as Data Structure.
Note:
=>We construct Data Structures in Java using Collection<E> and
Map<K,V>
==========================================================
Dt : 15/7/2022
PolyMorphism in Java:
=>The process in which the programming components having more
than one form is known as PolyMorphism.
Poly - Many
Morphism - Forms
=>This PolyMorphism is categorized into two types:
1.Dynamic PolyMorphism
2.Static PolyMorphism
1.Dynamic PolyMorphism:
=>The PolyMorphism at execution stage is known as Dynamic
PolyMorphism or Runtime PolyMorphism.
Ex:
Method Overriding process
Note:
=>Through Method Overriding process we can have more than one
form at execution stage,because of this reason Method Overriding
process comes under Dynamic PolyMorphism or Runtime PolyMorphism.
Diagram:
=====================================================
2.Static PolyMorphism:
=>The PolyMorphism at compilation stage is known as Static
PolyMorphism or Compile time PolyMorphism.
Ex:
Method Overloading process.
Note:
=>Through Method overloading process we can have more than
one method form by differntiating para_list or para_type at
compilation stage is known as Method Overloading process.
add(int)
add(int,int)
add(int,float)
Ex:
public class Addition {
public void add(int x) {
public void add(int x,int y) {
public void add(int x,float y) {
=====================================================
=>The compiler at compilation stage will control the following
keywords:
1.static
2.private
3.final
1.static:
=>The following are the static programming components:
a.static variables
b.static methods
c.static blocks
d.static classes
e.static Interfaces
f.static AbstractClasses
=>There is no concept of static constructors in Java.
Behaviour:
=>Static programming components will get the memory within the
class while class loading and can be accessed with class_name.
faq:
define Static binding?
=>The process of binding all the static components to class at
compilation stage is known as static binding or Compile time
binding.
faq:
define Dynamic binding?
=>The process of binding all the NonStatic components to object
while execution stage is known as Dynamic Binding or Runtime
Binding.
===================================================
*imp
2.private:
=>The following are the private programming components:
a.private variables
b.private methods
c.private constructors
d.private classes
=>There is no concept of private blocks,private Interfaces and
private abstract classes.
a.private variables:
=>The variables which are declared with private keyword are
known as private variables.
Behaviour:
=>private variables are accessed only inside the class,which
means accessed by the methods of same class.
Note:
=>In realtime private variables are used in Bean classes and
POJO classes. (POJO - Plain Old Java Object)
b.private methods:
=>The methods which are declared with private keyword are known
as private methods.
Behaviour:
=>Private methods are accessed only inside the class,which means
can be accessed by the NonPrivate methods of same class
Ex:
PTest.java
package test;
public class PTest {
private int z=100;
private void dis() {
System.out.println("====Private dis()====");
System.out.println("The value z:"+z);
}
public void access() {
this.dis();//Private method_call
}
}
DemoPoly1.java(MainClass
package maccess;
import test.PTest;
public class DemoPoly1 {
public static void main(String[] args) {
PTest ob = new PTest();
//ob.dis();//Compilation
ob.access();
}
}
o/p:
====Private dis()====
The value z:100
Diagram:
======================================================
*imp
c.private constructors:
=>The constructors which are declared with private keyword
are known as Private Constructors.
Behaviour:
=>private constructor is executed when the object is created
inside the same class where Private constructor is declared.
=>Private Constructor will restrict the object creation from
externally.
=======================================================
faq:
define SingleTon class?
=>The class which generate only one object is known as SingleTon
class.
faq:
define SingleTon class design pattern?
=>The process of constructing SingleTon class using the
following components is known as 'SingleTon class design pattern'.
(i)private static reference variable
(ii)private constructor
(iii)static method
(i)private static reference variable:
=>private static reference variable will hold the reference of
object created inside the same class.
(ii)private constructor:
=>private constructor will restrict the object creation from
externally and executed when object is created inside the same
class
(iii)static method:
=>This static method is used to access the object reference
outside the class.
-------------------------------------------------
=>Based on Object creation process the SingleTon class design
pattern is categorized into two types:
(a)Early Instantiation process
(b)Late Instantiation process
(a)Early Instantiation process:
=>In Early Instantiation process the object is created while
class loading.
=>In Early Instantiation process the object is created through
static block.
Ex:
CTest1.java
package test;
public class CTest1 {
private CTest1() {}
private static CTest1 ob = null;
static {
ob = new CTest1();//Con_Call
}
public static CTest1 getRef() {
return ob;
}
public void dis(int z) {
System.out.println("====dis(z)====");
System.out.println("The value z:"+z);
}
}
DemoPoly2.java(MainClass);
package maccess;
import test.CTest1;
public class DemoPoly2 {
public static void main(String[] args) {
//CTest1 ob = new CTest1();//Con_Call Error
CTest1 ob = CTest1.getRef();//Accessing the object_ref
ob.dis(123);
}
}
(b)Late Instantiation process:
=>In Late Instantiation process the object is created after
class loading.
=>In Late Instantiation process the object is created through
method.
Ex:
CTest2.java
package test;
public class CTest2 {
private CTest2() {}
private static CTest2 ob = null;
public static CTest2 getRef() {
if(ob==null)
{
ob = new CTest2();//Con_Call
}
return ob;
}
public void dis(int z) {
System.out.println("====dis(z)====");
System.out.println("The value z:"+z);
}
}
DemoPoly3.java(MainClass)
package maccess;
import test.CTest2;
public class DemoPoly3 {
public static void main(String[] args) {
//CTest2 ob = new CTest2();//Con_Call Error
CTest2 ob = CTest2.getRef();//Accessing the object_ref
ob.dis(123);
}
}
=============================================================
Dt : 16/7/2022
Note:
=>In realtime SingleTon class design pattern is used part of
DAO(Data Access Object) layer in MVC(Model View Controller) to
hold DataBase Connection code.
=============================================================
d.private classes:
=>The classes which are declared with private keyword are known
as private classes.
Rule:
=>Private classes can be declared only as InnerClasses,which
means OuterClasses cannot be private.
Note:
=>private InnerClass objects are created inside the NonPrivate
method of same class.
Diagram:
Ex:
SubClass1.java
package test;
public class SubClass1 {
private class SubClass2{
public void m2(int x) {
System.out.println("===m2(x)===");
System.out.println("The value x:"+x);
}
}//Private InnerClass
private static class SubClass3{
public void m3(int y) {
System.out.println("====m3(y)====");
System.out.println("The value y:"+y);
}
}//Private InnerClass
public void access(int x,int y) {
SubClass2 ob2 = new SubClass2();
SubClass3 ob3 = new SubClass3();
ob2.m2(x);
ob3.m3(y);
}
}//OuterClass
DemoPoly4.java(MainClass)
package maccess;
import test.SubClass1;
import java.util.*;
public class DemoPoly4 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the value of x:");
int x = s.nextInt();
System.out.println("Enter the value of y:");
int y = s.nextInt();
SubClass1 ob1 = new SubClass1();//OuterClass object
ob1.access(x, y);
s.close();
o/p:
Enter the value of x:
12
Enter the value of y:
13
===m2(x)===
The value x:12
====m3(y)====
The value y:13
==========================================================
*imp
3.final:
=>The following are the final programming components:
a.final Variables
b.final Methods
c.final Classes
=>There is no concept of final blocks,final constructors,final
Interfaces and final abstractClasses.
a.final Variables:
=>The Variables which are declared with 'final' keyword in
classes are known as final Variables.
Rule:
=>These final variables must be initialized with values and
once initialized cannot be modified.
Note:
=>These final variables are also known as Secured variables or
Constant Variables.
=>final variables in classes can be initialized using
constructor.
b.final Methods:
=>The methods which are declared with final keyword are known
as final methods.
Rule:
=>final methods cannot be Overrided.
c.final Classes:
=>The classes which are declared with final keyword are known
as final classes
Rule:
=>final classes cannot be extended,which means there is no
inheritance for final classes.
----------------------------------------------------
Note:
=>In realtime using final programming components we can construct
Immutable Classes.
faq:
define Immutable Classes?
=>The classes which are constructed using the following rules
are known as Immutable Classes:
Rule-1 : The class must be final class
Rule-2 : The variables declared in class must be private
and final
Rule-3 : The class must be declared with only 'Getter methods'.
Rule-4 : These 'Getter methods' must be final methods
Note:
=>These Immutable classes will generate Immutable objects.
Ex:
UserLogin.java
package test;
public final class UserLogin {
private final String userName,passWord;
public UserLogin(String userName,String passWord) {
this.userName = userName;
this.passWord = passWord;
}
public final String getUserName() {
return userName;
}
public final String getPassWord() {
return passWord;
}
}
DemoPoly5.java(MainClass)
package maccess;
import test.UserLogin;
import java.util.*;
public class DemoPoly5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the UserName:");
String uName = s.nextLine();
System.out.println("Enter the PassWord:");
String pWord = s.nextLine();
UserLogin ob = new UserLogin(uName,pWord);
//Immutable Object
System.out.println("UserName:"+ob.getUserName());
System.out.println("PassWord:"+ob.getPassWord());
s.close();
o/p:
Enter the UserName:
nit.v
Enter the PassWord:
mzu672
UserName:nit.v
PassWord:mzu672
---------------------------------------------------
Note:
=>In realtime Immutable Objects are used to record transaction
details in banking domain.
----------------------------------------------------
Note:
=>Based on security the objects are categorized into two types:
(i)Mutable Objects
(ii)Immutable Objects
(i)Mutable Objects:
=>The Objects once created can be modifed are known as Mutable
Objects.
(ii)Immutable Objects:
=>The Objects once created cannot be Modified are known as
Immutable Objects or Secured Objects or Constant objects
-------------------------------------------------------------
faq:
wt is the diff b/w
(i)Getter methods
(ii)Setter methods
(i)Getter methods:
=>Getter methods are used to get the data from the objects.
(ii)Setter methods:
=>Setter methods are used to set the data to the objects.
Rule of Setter and Getter methods:
=>Every variable which is declared in classes will have its
own Setter method and Getter method.
==============================================================