Java is a high-level programming language, Object-Oriented language.
This is a very simple and easy to use Language It is a very popular language used in the software development field. We know the various software like Windows Application or Web Application is made using Java. Due to its Platform independent feature, it is widely used all over the world. The security feature of Java due to byte code make this language as a secure language. It has a large scope in Software. As every device use java to run Applications.
Scope of Java
Today we use Hadoop to solve Big Data Problems, Which also uses Java mostly to make programs on Map Reduce. In Java, everything is an object. If we talk about array in java, the array is also an object in java. A string is a class in Java, we create string object to assign values to the string object. Without creating the object we can not access the features of the class. The array is a class in Java, so there is a number of built-in methods in Array class which we only call on an array object. So we can say that Java is a purely Object-Oriented Programming Language.
Garbage Collection
In terms of memory, Java has the concept of garbage collection which makes Java as a memory saving Language. A garbage collector is a program which is called by JVM (Java Virtual Machine) . Garbage collector destroys the space allocated by unused objects in the program.
Portable
Java is portable. It can run on any platform, So Java is platform-independent language. In java, when a program is compiled, the byte code is generated. This byte code is platform-independent code and can be run on any platform like windows, Linux, Mac, etc. JVM is platform dependent and fro every platform JVM is different. The same byte code is shared among all the platforms and can be run on a variety of machines. This makes Java as a Platform Independent Language. That’s why Java is so popular language.
JVM is Java virtual machine that is an abstract machine which runs the byte code, we call it interpreter.JRE (Java Runtime Environment) is the implementation of JVM. JRE provides the environment to run the java program. JDK is the Java Development kit. JDK consists of JRE and JVM. JDK provides a programmer the facility to develop Java Program. Without JDK it is not possible to develop programs in Java. In other words, JDK is the collection of JRE and other Development tools like javac and java.
Java is a secure Language. Byte code makes the java program more secure. Byte code is the object code that can not be read by a human. So you need JVM to understand byte code.
Encapsulation
Encapsulation and Data hiding are the two important features of Java. Encapsulation means to enclose data members and member functions in the form of a unit. This encapsulated unit is called as a class. Class is a blueprint or a template used to create objects of the class.
Example to create a class and an Object
class MyClass // Class Definition
{
private int name,age; // Data members
public void getDetails() // Member methods
{
name=”Ravi”;
age=24;
}
public void showDetails()
{
System.out.println(“Name is:”+name+”age is:”+age);
}
public static void main(String args[]) // Main function definition
{
MyClass m=new MyClass(); // Object Declaration
m.getDetails(); // Calling of member methods through objects
m.showDetails();
}
Objects are the real-world entity which has some features. Objects are created using the class name. Every object is different from each other and each object has state and behavior. As we know in Java, everything is an object. Without creating an object member of the class can not be accessed and the data of the class is hidden from the outside world. This concept is called Data Hiding. This feature provides security to our data.
Example of access specifiers
class Employee
{
private int salary; // private is access specifiers, private data can not be accessed outside class
protected int age; //Protected data can be accessed within same package
public String name; //Public data can be accessed anywhere in the program and outside package
}
For Free Demo classes Call: 8237077325
Registration Link:Click Here!
Multi-Threading
Java supports multithreading. Multithreading is the way to achieve multitasking. One program is divided in subparts and all subparts are executed simultaneously. These subparts are called the threads. Using this feature we can make our program more efficient and fast. Multithreading concept can be used in making animation and games.
Example of Threading concept
In two ways we can create a thread class:-
1) Extending the Thread class using extends keyword
2) Implementing the Runnable interface
1) Create a thread using extends keyword
class MyThread extends Thread
{
public void run( )
{
for(int i=0;i<10;i++) { System .out. println(“Hello”+i);
}
}
class Example
{
public static void main(String args[])
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
t1.start( );
t2.start();
}
}
In the above example threads, two threads are created using extends keyword. t1 and t2 are the threads which call the start method and starts executing the run method. Both the threads run in parallel.
2) Create a thread using implements keyword
class MyThread implements Runnable
{
public void run( ) // Overridden method from Runnable interface
{
for(int i=0;i<10;i++) { System .out . println(“Hello”+i);
}
}
class Example
{
public static void main(String args[])
{
MyThread t1=new MyThread(); // creation of objects of class Example
MyThread t2=new MyThread();
Thread th1=new Thread(t1); // creation of thread objects t1 and t2
Thread th2=new Thread(t2);
th1.start( ); // calling of start method to run the thread
th2.start();
}
}
Inheritance
Java supports inheritance. Inheritance provides the reusability of the code that is previously developed by any programmer. It is a time saving and effort saving concept. In inheritance, there are Parent and child classes. child classes are the derived classes which can be created using Parent classes. In Java Object class is the parent class of all the classes which we create. Object class is defined in a package named as java.lang. In java, multiple inheritances are not supported because it creates ambiguity problem but there is a solution for this is an interface.
There is various type of inheritances in java. They are as follows:-
1) Single Level 2) Multilevel 3) Hierarchical
For Free Demo classes Call: 8237077325
Registration Link:Click Here!
1) Single level Inheritance
When a single class inherits the properties from the base class , It is known as single level inheritance.
Example on Single Inheritance
class Base
{
public void show(){ System . out . println(“Show called”);
}
class Derived extends Base
{
public static void main(String args[])
{
Derived d=new Derived();
d.show();
}
}
2)Multilevel Inheritance
When a class is inherited by any class which is also inherited by another class is known as Multilevel inheritance.
Example on multilevel inheritance
class A
{
public void show(){ System . out . println(“Show called”);
}
Class B extends A
{
}
class C extends A
{
public static void main(String args[])
{
C obj=new C();
obj .show();
}
}
3)Hierarchical Inheritance
When a class has more than one derived classes is known as Hierarchical Inheritance.
Example of Hierarchical Inheritance
Class Shape
{
public int x , y;
Shape() { x=20; y=30; } // Default Constructor
}
class Rectangle extends Shape
{
public void getArea()
{
int area=x * y;
system.out.println(“Area is :” + area);
}
}
class Circle extends Shape
{
double pi;
public void getArea()
{
pi=3.14;
double area = pi* x*x;
System. out. println(“Area: “+area);
}
}
class MainClass
{
public static void main(String args[])
{
Rectangle r=new Rectangle();
Circle c=new Circle();
- getArea();
- getArea();
}
}
Interface
The interface is a very important concept in java, which provides full abstraction. Abstraction is the process of hiding implementation details from the user and showing only the functionality. There is also a concept of Abstract class but it gives you the partial abstraction. We can implement the functionality of the abstract classes and interfaces in the child classes and it makes child classes more specific in nature.
The interface is used in many real-world projects. Using this concept, abstraction can be achieved.
Example on Interface
interface car // Interface creation
{
public void getspeed(); // abstract method getspeed()
}
class Honda implements car // implementation of interface car in Honda
{
public void getspeed()
{
System . out. println(“Speed = 100 kmph”);
}
}
class Mercedes implements car // implementation of interface car in Mercedes
{
public void getspeed()
{
System . out . println(“Speed = 120 kmph”);
}
}
class Speed
{
public static void main(String args[])
{
car c=new Honda();
- getspeed();
c=new Mercedes();
- getspeed();
}
}
Multiple Inheritance can be achieved by using interface
interface color
{
public void fillColor();
}
class Rectangle
{
public void fillRectangle()
{
system. out. println(“This is to be filled with a color”);
}
}
class MultipleExample extends Rectangle implements color
{
public void fillcolor()
{
String col= “Green”;
System. out. println(“The color is “+ col);
}
public static void main(String args[])
{
MultipleExample m=new MultipleExample();
- fillRectangle();
- fillcolor();
}
}
For Free Demo classes Call: 8237077325
Registration Link:Click Here!
Polymorphism
Polymorphism is a feature we use in Java. It is of two types: Compile time and Run time polymorphism. Method overloading is the example of compile-time while Method overriding is an example of run time polymorphism. Polymorphism means many forms. If we want to perform a single task in multiple ways than is it achieved using polymorphism. It is the most important feature of java.
Example on Compile Time Polymorphism
class Overloading
{
public void add(int a,int b) // Overloaded method add which takes 2 parameters
{
System. out. println(a+b);
}
public void add(int a,int b,int c) // Overloaded method add which takes 3 parameters
{
System. out. println(a+b+c);
}
public static void main(String args[])
{
Overloading obj=new Overloading();
obj.add(10,20);
obj.add(10,20,30);
}
Example of Run Time Polymorphism
class Overriding
{
public void show()
{
System.out.println(“Calling Show in Base Class”);
}
}
class Inherited extends Overriding
{
public void show()
{
System.out.println(“Calling Show in Derived Class”);
}
public static void main(String args[])
{
Inherited obj=new Inherited();
obj.show();
}
}
Difference between Method Overloading and Method Overriding
1) Method overloading is an example of compile-time polymorphism while Method Overriding is an example of run time polymorphism.
2) method overloading is done within the same class while Method overriding concept is applied in case of inheritance.
3) In method Overloading method signature should not be same while In method Overriding method signature should be the same.
Strongly-Typed Language
Java is a strongly typed language .If we want to assign a value to a variable then the type of that value must be defined to the compiler before assigning .There are two types of casting in Java. Implicit and explicit type casting. Implicit type casting is automatic type conversion where the data type of smaller size can be automatically assigned to a larger type of data and that is done by compiler. While in case of Explicit type casting , larger type of data is assigned to smaller type on specifying the type in which conversion is taking place. Explicit type casting is called narrowing and implicit type casting is called widening.
Example on Implicit type casting
int i=3;
long l=i; // int i is automatically type casted to long l
Example on Explicit type casting
long l=4;
int i=(int) l; // long l is explicitly type cast to int i on writing cast operator
Collection
Collections in java play an important role to work with objects. Using Collections we can perform operations on data like searching, sorting, insertion, deletion, and manipulation easily.
Various Type of collections are : Set, List , Queue
Map is not a true collection
We can take an example of ArrayList class which implements List interface. Lets see how elements are added in the list using ArrayList
Example to create a list using ArrayList class
class CollectionExample
{
ArrayList al=new ArrayList();
al.add(“Red”);
al.add(“Green”);
al.add(“Blue”);
Iterator it=al.iterator();
while(it.hasNext())
{
System. out. println(it.next());
}
}
For Free Demo classes Call: 8237077325
Registration Link:Click Here!
Call the Trainer and Book your free demo Class for JAVA now!!!
© Copyright 2019 | Sevenmentor Pvt Ltd.