Java is a high level programming language developed by Sun Microsystems. A small team of engineers worked together called Green Team, initiated the language in 1991. Today Java is a mostly used language for developing and delivering content in all many domains.
Now let’s see the programming concepts involved !
What is Java?
- Object Oriented – Java is fully based on object so it is extensible.
- Platform independent – Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code.
- Simple – Easy to learn , if you understand OOPS Concept , then java is easy to learn.
- Secure – Java’s secure feature, it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
- Portable – Being architectural-neutral and having no implementation dependent aspects of the specification makes Java portable.
- Robust – Java takes some steps to eliminate error by using some techniques like error checking and runtime checking.
OOPS Concept
Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:
- Classes
- Objects
- Polymorphism
- Inheritance
- Encapsulation
- Abstraction
Class
Collection of objects is called class. It is a logical entity.
Object
Objects have states and behaviour. An object is an instance of a class.
For example :
class Rect
{
Private int l,b;
public void setDimension(int x,int y)
{
l=x;
b=y;
}
public int area()
{
return l*b;
}
public void display()
{
System.out.println(“Length=”+l);
System.out.println(“Breadth=”+b);
}
public static void main(String ac[])
{
Rect r=new Rect();
r.setDimension(5,10);
r.display();
System.out.println(“Area=”+r.area());
}
}
Polymorphism
A process is performed in more than one ways
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation
Inheritance
When one object acquires all the properties and behaviours of parent object i.e. known as inheritance.
For example :
class Human extends men {
// new fields and methods defining a human would go here
}
Abstraction
Hiding internal details and showing functionality is known as abstraction.
String
Java provides a lot of concepts that can be performed on a string such as compare, concat, equals, split, length, replace, compareTo, intern, substring, etc.
string is a sequence of characters. But in java, string is an object that represents a sequence of characters. String class is used to create string object.
Creating String
The most direct way to create a string is to write:
1. String Literal
For example :
String s2=”Welcome”; //will not create new instance
Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn’t exist in the pool, a new string instance is created and placed in the pool.
2. By New Keyword
For example :
String s= String(“Welcome”); //creates two objects and one reference variable
char[] hello = { ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘.’};
String helloString = new String(hello);
System.out.println(helloString);
The last line of this code snippet displays hello.
Access Specifier
One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this class available only through methods. Java allows you to control access to classes, methods, and fields via so-called access specifiers
Java offers four access specifiers, listed below in decreasing accessibility:
- Public
- protected
- Default ( no specifiers )
- private
Public
public classes, methods, and fields can be accessed from everywhere. The only constraint is that a file with Java source code can only contain one public class whose name must also match with the filename.
For example :
public class Square // public class
{
public x, y, size; // public instance variables
}
Protected
protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package, but not from anywhere else.
Default (no specifiers)
If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. This access-level is convenient if you are creating packages
Private
private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses. So, the private access specifier is opposite to the public access specifier.
For example :
public class Square { // public class
private double x, y // private (encapsulated) instance variables
public setCorner(int x, int y) { // setting values of private fields
this.x = x;
this.y = y;
}
public getCorner() { // setting values of private fields
return Point(x, y);
}
}
Package
Package = directory. Java classes can be grouped together in packages. A package name is the same as the directory (folder) name which contains the .java files. You declare packages when you define your Java program, and you name the packages you want to use from other libraries in an import statement.
Package Declaration
The first statement, other than comments, in a Java source file, must be the package declaration.
Following the optional package declaration, you can have import statements, which allow you to specify classes from other packages that can be referenced without qualifying them with their package.
Default package – Although all Java classes are in a directory, it’s possible to omit the package declaration. For small programs it’s common to omit it, in which case Java creates what it calls a default package. Sun recommends that you do not use default packages.
Package declaration Syntax
The statement order is as follows. Comments can go anywhere.
- Package statment (optional).
- Imports (optional).
- Class or interface definitions.
Interface
There are mainly three reasons to use interface. They are given below.
- It is used to achieve fully abstraction.
- By interface, we can support the functionality of multiple inheritance.
- It can be used to achieve loose coupling.
For example :
interface Print {
void p1();
}
interface Show {
void s1();
}
class VK implements Print,Show {
public void print(){System.out.println(“Hello”);
}
public void show(){System.out.println(“Welcome”);
}
public static void main(String args[])
{
VK obj = new VK();
obj.print();
obj.show();
}