Java Features
The primary objective of Java programming language creation was
to make it portable, simple and secure programming language.
• Simple
• Object-Oriented
• Portable
• Platform independent
• Secured
• Robust
• Architecture neutral
• Interpreted
• High Performance
• Multithreaded
• Distributed
• Dynamic
Object-oriented programming
• Java is an object-oriented programming language. Everything in
Java is an object. Object-oriented means we organize our software
as a combination of different types of objects that incorporates
both data and behavior.
• Object-oriented programming (OOPs) is a methodology that
simplifies software development and maintenance by providing
some rules.
Basic concepts of OOPs are:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Objects
• An object in Java is the physical as well as a
logical entity, whereas, a class in Java is a
logical entity only.
Objects
An object has three characteristics:
• State: represents the data (value) of an object.
• Behavior: represents the behavior (functionality)
of an object such as deposit, withdraw, etc.
• Identity: An object identity is typically
implemented via a unique ID. The value of the ID
is not visible to the external user. However, it is
used internally by the JVM to identify each object
uniquely.
• For Example, Pen is an object. Its name is
Reynolds; color is white, known as its state. It is
used to write, so writing is its behavior.
Class
• A class is a user defined prototype from which objects are
created.
• It represents the set of properties or methods that are
common to all objects of one type. In general, class
declarations can include these components, in order:
– Modifiers : A class can be public or has default access
– Class name: The name should begin with a initial letter
(capitalized by convention).
– Superclass(if any): The name of the class’s parent (superclass), if
any, preceded by the keyword extends. A class can only extend
(subclass) one parent.
– Interfaces(if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
– Body: The class body surrounded by braces, { }.
Class
An Example:-
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
Inheritance
• Inheritance in Java is a mechanism in which
one object acquires all the properties and
behaviors of a parent object.
Inheritance
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Polymorphism
• Polymorphism in Java is a concept by which we
can perform a single action in different ways.
Polymorphism is derived from 2 Greek words:
poly and morphs. The word "poly" means many
and "morphs" means forms. So polymorphism
means many forms.
• Types of Polymorphism –
Runtime and compile time
Polymorphism
• Runtime Polymorphism example:
public class Animal
{
public void sound()
{
System.out.println("Animal is making a sound");
}
}
Polymorphism
class Horse extends Animal
{ @Override
public void sound()
{
System.out.println("Neigh");
}
public static void main(String args[])
{
Animal obj = new Horse();
obj.sound();
}
}
Output:- Neigh
Polymorphism
Compile time Polymorphism
class Overload
{
void demo (int a)
{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a)
{
System.out.println("double a: " + a);
return a*a;
}
}
Polymorphism
class MethodOverloading
{
public static void main (String args [])
{
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
}
} Output:-
a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25