Java OOPS/Fundamental concepts step by step in details
OOPS concepts in Java
Java tutorials for beginners
Java basics concepts
Java Objects and Classes
What is Inheritance in Java?
What is Abstraction in Java?
What is Interface in Java?
What is Abstract Class in Java?
What is Polymorphism in Java?
What is Encapsulation in Java?
Java programming example
How Java Program Work?
Hello World in Java
Java basics for beginners
Java is a object oriented programming language Java supports the following fundamental concepts
1. Objects
2. Classes
3. Instance
4. Polymorphism
5. Inheritance
6. Encapsulation
7. Abstraction
1. Objects:
Object is an instance of a class
and entity that has state and behaviour e.g. pencil, book, table, car. State
is value of an object and behaviour is functionality of an object such as
writing.
2. Classes:
Class is a template or blueprint that describe the
behaviours and states of a particular entity. A class defines new data type and
can be used to create object of that type.
A class is declared using class keyword and it
can be either abstract, final or default normal class.. The variables defined
within a class are called instance variables and instance variables and methods
are known as class members.
Class declarations can include
following order
o
Modifiers : A class can be
public or default access specifier.
o
Class name: The name should begin with a
initial capital letter.
o
Super class: Can use extends keyword for
inherit.
o
Interfaces: Can use comma separated
implements keyword for inherit.
o
Body
: The class body
surrounded by braces, { }.
Example:
public class Student {
String name;
String rollNo;
int age;
String address;
}
Here Student is a class and name, rollNo, age, address are properties of
Student class.
3. Instance:
An instance is
a copy of a Class that representing an Object. When a new instance of a class
is created, the JVM will allocate memory for that class instance.
Example of
create instance of class.
Student std = new Student();
Here std is instance of Student class, new
keyword creates the physical copy of the object and assign it to
the std variable.
4. Polymorphism:
Polymorphism is the ability to many
forms of an object. The most common use of Polymorphism is parent class
reference is used to a child class object.
There are two types of Polymorphism in java
1. Objects:
Object is an instance of a class and entity that has state and behaviour e.g. pencil, book, table, car. State is value of an object and behaviour is functionality of an object such as writing.
2. Classes:
Class is a template or blueprint that describe the
behaviours and states of a particular entity. A class defines new data type and
can be used to create object of that type.
A class is declared using class keyword and it can be either abstract, final or default normal class.. The variables defined within a class are called instance variables and instance variables and methods are known as class members.
A class is declared using class keyword and it can be either abstract, final or default normal class.. The variables defined within a class are called instance variables and instance variables and methods are known as class members.
Class declarations can include following order
o
Modifiers : A class can be
public or default access specifier.
o
Class name: The name should begin with a
initial capital letter.
o
Super class: Can use extends keyword for
inherit.
o
Interfaces: Can use comma separated
implements keyword for inherit.
o
Body
: The class body
surrounded by braces, { }.
Example:
public class Student {
String name;
String rollNo;
int age;
String address;
}
Here Student is a class and name, rollNo, age, address are properties of Student class.
3. Instance:
An instance is
a copy of a Class that representing an Object. When a new instance of a class
is created, the JVM will allocate memory for that class instance.
Example of
create instance of class.
Student std = new Student();
Here std is instance of Student class, new
keyword creates the physical copy of the object and assign it to
the std variable.
4. Polymorphism:
Polymorphism is the ability to many
forms of an object. The most common use of Polymorphism is parent class
reference is used to a child class object.
There are two types of Polymorphism in java
1. Compile time Polymorphism (method overloading).
2. Runtime Polymorphism (method overriding).
1. Compile time
Polymorphism (method overloading).
Overloading is
compile time polymorphism where more than one methods share the same name with
different parameters or signature.
Example:
class TestOverloading
{
public void
display(char c)
{
System.out.println(c);
}
public void
display(char c, int num)
{
System.out.println(c
+ " "+num);
}
}
class TestMainClass
{
public static
void main(String args[])
{
TestOverloading
obj = new TestOverloading ();
obj. display('a');
obj. display('a',10);
}
}
2.
Runtime
Polymorphism (method overriding).
Overriding is run
time polymorphism having same method with same
parameters or signature, but associated in a class & its
subclass.
Example:
class Eat{
//Overridden method
public void eat()
{
System.out.println("Eating");
}
}
class Rice extends Eat {
//Overriding method
public void eat(){
System.out.println("He is eating rice");
}
public static void main( String args[]) {
Rice obj = new Rice ();
//This will call the child class version of eat()
obj.eat();
}
}
5. Inheritance:
The process by which one class access the properties, data and methods
of another class is called inheritance
The purpose of inheritance is re-usability code so that a class has to
write only the features and rest of the common properties and functionalities
can be extended from the another class.
importance
in Inheritance
1. Sub class or Child Class.
The class that extends another class and use the features is known as
Sub class or Child class.
2. Super Class or Parent Class.
The inherited class whose properties and functionalities are using in
another class is known as Parent class / Super class or Base class.
Example:
class class Calculator
{
int value;
public void sum(int x, int y) {
value = x + y;
System.out.println("The sum of the given numbers is "+value);
}
public
void difference(int x, int y) {
value = x - y;
System.out.println("The difference between the given numbers is
"+value);
}
}
public class
MainCalculator extends Calculator {
public
void multiple(int x, int y) {
value = x * y;
System.out.println("The multiple of the given numbers is "+value);
}
public
static void main(String args[]) {
int a = 30, b = 10;
MainCalculator obj = new MainCalculator ();
obj.sum(a, b);
obj.difference(a, b);
obj.multiple(a, b);
}
}
Types of Inheritance
1. Compile time Polymorphism (method overloading).
Overloading is compile time polymorphism where more than one methods share the same name with different parameters or signature.
Example:
class TestOverloading
{
public void
display(char c)
{
System.out.println(c);
}
public void
display(char c, int num)
{
System.out.println(c
+ " "+num);
}
}
class TestMainClass
{
public static
void main(String args[])
{
TestOverloading
obj = new TestOverloading ();
obj. display('a');
obj. display('a',10);
}
}2. Runtime Polymorphism (method overriding).
Overriding is run time polymorphism having same method with same parameters or signature, but associated in a class & its subclass.
Example:
class Eat{
//Overridden method
public void eat()
{
System.out.println("Eating");
}
}
class Rice extends Eat {
//Overriding method
public void eat(){
System.out.println("He is eating rice");
}
public static void main( String args[]) {
Rice obj = new Rice ();
//This will call the child class version of eat()
obj.eat();
}
}5. Inheritance:
The process by which one class access the properties, data and methods
of another class is called inheritance
The purpose of inheritance is re-usability code so that a class has to
write only the features and rest of the common properties and functionalities
can be extended from the another class.
importance
in Inheritance
1. Sub class or Child Class.
The class that extends another class and use the features is known as
Sub class or Child class.
2. Super Class or Parent Class.
The inherited class whose properties and functionalities are using in
another class is known as Parent class / Super class or Base class.
Example:
class class Calculator
{
int value;
public void sum(int x, int y) {
value = x + y;
System.out.println("The sum of the given numbers is "+value);
}
public
void difference(int x, int y) {
value = x - y;
System.out.println("The difference between the given numbers is
"+value);
}
}
public class
MainCalculator extends Calculator {
public
void multiple(int x, int y) {
value = x * y;
System.out.println("The multiple of the given numbers is "+value);
}
public
static void main(String args[]) {
int a = 30, b = 10;
MainCalculator obj = new MainCalculator ();
obj.sum(a, b);
obj.difference(a, b);
obj.multiple(a, b);
}
}
Types of Inheritance
1. Single
Inheritance.
2. Multilevel Inheritance.
1. Single Inheritance
In single
inheritance, sub classes inherit the features of one super class.
Example:
class class Calculator
{
int value;
public void sum(int x, int y) {
value = x + y;
System.out.println("The sum of the given numbers is "+value);
}
public
void difference(int x, int y) {
value = x - y;
System.out.println("The difference between the given numbers is
"+value);
}
}
public class
MainCalculator extends Calculator {
public
void multiple(int x, int y) {
value = x * y;
System.out.println("The multiple of the given numbers is "+value);
}
public
static void main(String args[]) {
int a = 30, b = 10;
MainCalculator obj = new MainCalculator ();
obj.sum(a, b);
obj.difference(a, b);
obj.multiple(a, b);
}
}
2.
Multilevel Inheritance.
In Multilevel Inheritance, a subclass/ derived class inherit
the features of more than one class.
Java does not support multiple inheritance with classes. We can achieve
multiple inheritance only through Interfaces.
Example:
public interface InterfaceIA {
public void sum(int x,int z);
}
public interface InterfaceIB {
public void subtraction(int x,int z);
}
public class Impl implements InterfaceIA,
InterfaceIB, InterfaceIC {
@Override
public void sum(int x,int z) {
System.out.println("Addition is "+(x+z));
}
@Override
public void substraction(int x,int z) {
System.out.println("Subtraction is "+(x-z))
}
public static void main(String[] args) {
Impl obj= new Impl();
obj.sum();
obj.subtraction();
}
}
1. Single Inheritance
In single
inheritance, sub classes inherit the features of one super class.
Example:
class class Calculator
{
int value;
public void sum(int x, int y) {
value = x + y;
System.out.println("The sum of the given numbers is "+value);
}
public
void difference(int x, int y) {
value = x - y;
System.out.println("The difference between the given numbers is
"+value);
}
}
public class
MainCalculator extends Calculator {
public
void multiple(int x, int y) {
value = x * y;
System.out.println("The multiple of the given numbers is "+value);
}
public
static void main(String args[]) {
int a = 30, b = 10;
MainCalculator obj = new MainCalculator ();
obj.sum(a, b);
obj.difference(a, b);
obj.multiple(a, b);
}
}
2. Multilevel Inheritance.
In Multilevel Inheritance, a subclass/ derived class inherit
the features of more than one class.
Java does not support multiple inheritance with classes. We can achieve
multiple inheritance only through Interfaces.
Example:
public interface InterfaceIA {
public void sum(int x,int z);
}
public interface InterfaceIB {
public void subtraction(int x,int z);
}
public class Impl implements InterfaceIA,
InterfaceIB, InterfaceIC {
@Override
public void sum(int x,int z) {
System.out.println("Addition is "+(x+z));
}
@Override
public void substraction(int x,int z) {
System.out.println("Subtraction is "+(x-z))
}
public static void main(String[] args) {
Impl obj= new Impl();
obj.sum();
obj.subtraction();
}
}
6. Encapsulation:
Encapsulation is the process of wrap the data and
code into single unit. The variables of a class will be hidden from other
classes, and can be accessed by methods of their current class.
Make encapsulation in following way.
1. Declare variables as private.
2. Declare public setter and getter methods to
access or modify the value.
Example:
public class Employee {
private
int id;
private
String name;
private
int age;
public
int getId() {
return
id;
}
public
void setId(int id) {
this.id
= id;
}
public
String getName() {
return
name;
}
public
void setName(String name) {
this.name
= name;
}
public
int getAge() {
return
age;
}
public
void setAge(int age) {
this.age
= age;
}
}
7. Abstraction:
Abstraction is the process of hiding the implementation details, only showing the functionality.
In other words, Abstraction is the process to reduce complexity and taking out the important characteristics of a class.
There are two ways to achieve abstraction in java
1. Abstract class
1. Abstract class
2. Interface
1. Abstract class
Abstract class
defines a class that contains the abstraction without providing a complete
implementation of method. The abstract keyword is used for abstract class and
abstract method.
Abstract classes are used if you want to create a super class
that only defines the variables and methods and sub class will be accessed the
variables and methods for implementation.
Abstract class
defines a class that contains the abstraction without providing a complete
implementation of method. The abstract keyword is used for abstract class and
abstract method.
Abstract classes are used if you want to create a super class
that only defines the variables and methods and sub class will be accessed the
variables and methods for implementation.
Rule of Abstract class.
1. Abstract classes may or may not contain abstract methods.
2. Must be
declared abstract keyword if a class has at least one abstract method.
3. The class
cannot be instantiated if abstract is declared.
4. Must be
provided implementations of all the abstract methods, If you inherit
an abstract class.
Example:
abstract class Figure {
double dim1;
double dim2;
Figure(double d1,double d2){
dim1
= d1;
dim2
= d2;
}
abstract void area();
}
class Rectangle extends Figure{
Rectangle(double d1,douple d2){
super(d1,d2);
}
//Override area of rectangle
void area(){
System.out.println("area
of rectangle is"+ dim1*dim2);
}
}
class Demo{
public static void main(String
args[]){
Figure
f = new Rectangle(4,7);
f.area();
}
Output:
area
of rectangle is 28
2. Interface
1. Abstract classes may or may not contain abstract methods.
2. Must be
declared abstract keyword if a class has at least one abstract method.
3. The class
cannot be instantiated if abstract is declared.
4. Must be
provided implementations of all the abstract methods, If you inherit
an abstract class.
Example:
abstract class Figure {
double dim1;
double dim2;
Figure(double d1,double d2){
dim1
= d1;
dim2
= d2;
}
abstract void area();
}
class Rectangle extends Figure{
Rectangle(double d1,douple d2){
super(d1,d2);
}
//Override area of rectangle
void area(){
System.out.println("area
of rectangle is"+ dim1*dim2);
}
}
class Demo{
public static void main(String
args[]){
Figure
f = new Rectangle(4,7);
f.area();
}
Output:
area
of rectangle is 28
2. Interface
The
interface in java provides abstraction. We can create only abstract
method in interface and can’t create implementation or method body. It
is used to achieve abstraction and multiple inheritance in Java. It has
static constants and abstract methods.
Rule of Interface
1. All interface Methods are implicitly public and abstract
2. All variables in Interface by default constant(public, static, final).
3. Interface Methods can not be static.
4. Interface Methods can not be final.
5. One Interface can extend one or more Interface.
Example:
interface
Area
{
final static float pi = 3.14f;
float findArea(float x);
}
class Circle implements Area
{
public float findArea(float x)
{
return (x*x*xpi);
}
}
class Square implements Area
{
public float findArea(float x)
{
return(x*x);
}
}
public class InterFaceDemo
{
public static void main(String args[])
{
Circle c = new Circle();
Square s= new Square();
Area area;
area = c;
System.out.println("Area of Circle
is" + area.findArea(10));
area = s;
System.out.println("Area of Square
is" + area.findArea(30));
}
}
The
interface in java provides abstraction. We can create only abstract
method in interface and can’t create implementation or method body. It
is used to achieve abstraction and multiple inheritance in Java. It has
static constants and abstract methods.
Rule of Interface
1. All interface Methods are implicitly public and abstract
2. All variables in Interface by default constant(public, static, final).
3. Interface Methods can not be static.
4. Interface Methods can not be final.
5. One Interface can extend one or more Interface.
Example:
interface
Area
{
final static float pi = 3.14f;
float findArea(float x);
}
class Circle implements Area
{
public float findArea(float x)
{
return (x*x*xpi);
}
}
class Square implements Area
{
public float findArea(float x)
{
return(x*x);
}
}
public class InterFaceDemo
{
public static void main(String args[])
{
Circle c = new Circle();
Square s= new Square();
Area area;
area = c;
System.out.println("Area of Circle is" + area.findArea(10));
area = s;
System.out.println("Area of Square is" + area.findArea(30));
}
}
{
final static float pi = 3.14f;
float findArea(float x);
}
class Circle implements Area
{
public float findArea(float x)
{
return (x*x*xpi);
}
}
class Square implements Area
{
public float findArea(float x)
{
return(x*x);
}
}
public class InterFaceDemo
{
public static void main(String args[])
{
Circle c = new Circle();
Square s= new Square();
Area area;
area = c;
System.out.println("Area of Circle is" + area.findArea(10));
area = s;
System.out.println("Area of Square is" + area.findArea(30));
}
}