OOPs concept in java
The full form of OOPs is Object Oriented Programming System. Objects means the real word entity such as table,chair,blackboard etc. Object Oriented Programming is a methodology to design a program using class and objects . The object oriented paradigm supports the following principles.
Class : A class is a template for creating different objects which defines its
properties and behaviors. A class can contain fields and methods to
describe the behavior of an object.
Example :
public class First {
int a=10;
int b=12;
public int getTotal() {
return (a + b );
}
}
Object : Any entity that has state and behavior is known as an object.
Example :
public class First {
int a=10;
int b=12;
public int getTotal() {
return (a + b );
}
public static void main(String[] args) {
First first; // create a reference of class First
first=new First(); // create an object of class First
System.out.println("Sum of two integer is : " + first.getTotal());
}
}
Inheritance : The class which is inherits the properties of other class is known
as subclass(child class,derived class) and the class whose
property is inherited is known as superclass(parent class).
"extends" is the keyword which is used to inherits the properties
from parent class to child class.
Example :
public class First {
public void Total(int a, int b) {
System.out.println("Sum of two integer is : " +(a+b));
}
}
public class Second extends First {
public static void main(String[] args) {
Second second; // create a reference of class Second
second=new Second(); // create an object of class Second
second.Total(10,20);
}
}
Polymorphism : When a task is executed in different ways is known as
Polymorphism. There are two types of Polymorphism in java.
1. Compile time polymorphism (static binding or method
overloading) 2. Run time Polymorphism (dynamic binding or
method overriding).
Example of Compile time polymorphism (static binding or
method overloading) :
class First
{
public void display(char c)
{
System.out.println(c);
}
public void display(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Second
{
public static void main(String args[])
{
First obj = new First();
obj.display('a');
obj.display('a',10);
}
}
Example of Run time Polymorphism (dynamic binding or
method overriding) :
public class A {
public void callme()
{
System.out.println("A");
}
}
class D extends A{
public void callme()
{
System.out.println("B");
}
}
class C extends A{
public void callme()
{
System.out.println("C");
}
}
public class Demo
{
public static void main(String[] args) {
A a1 = new C();
a1.callme(); //Prints C
A a2 = new D();
a2.callme(); //Prints B
}
}
Encapsulation : Wrapping up of data and code into a single unit is called
Encapsulation.
Abstraction : hide the internal details and show the essential details of the
object is called as Abstraction or data hiding in java .
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
YouTube : Android & Java Tutorial
Class : A class is a template for creating different objects which defines its
properties and behaviors. A class can contain fields and methods to
describe the behavior of an object.
Example :
public class First {
int a=10;
int b=12;
public int getTotal() {
return (a + b );
}
}
Object : Any entity that has state and behavior is known as an object.
Example :
public class First {
int a=10;
int b=12;
public int getTotal() {
return (a + b );
}
public static void main(String[] args) {
First first; // create a reference of class First
first=new First(); // create an object of class First
System.out.println("Sum of two integer is : " + first.getTotal());
}
}
Inheritance : The class which is inherits the properties of other class is known
as subclass(child class,derived class) and the class whose
property is inherited is known as superclass(parent class).
"extends" is the keyword which is used to inherits the properties
from parent class to child class.
Example :
public class First {
public void Total(int a, int b) {
System.out.println("Sum of two integer is : " +(a+b));
}
}
public class Second extends First {
public static void main(String[] args) {
Second second; // create a reference of class Second
second=new Second(); // create an object of class Second
second.Total(10,20);
}
}
Polymorphism : When a task is executed in different ways is known as
Polymorphism. There are two types of Polymorphism in java.
1. Compile time polymorphism (static binding or method
overloading) 2. Run time Polymorphism (dynamic binding or
method overriding).
Example of Compile time polymorphism (static binding or
method overloading) :
class First
{
public void display(char c)
{
System.out.println(c);
}
public void display(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Second
{
public static void main(String args[])
{
First obj = new First();
obj.display('a');
obj.display('a',10);
}
}
Example of Run time Polymorphism (dynamic binding or
method overriding) :
public class A {
public void callme()
{
System.out.println("A");
}
}
class D extends A{
public void callme()
{
System.out.println("B");
}
}
class C extends A{
public void callme()
{
System.out.println("C");
}
}
public class Demo
{
public static void main(String[] args) {
A a1 = new C();
a1.callme(); //Prints C
A a2 = new D();
a2.callme(); //Prints B
}
}
Encapsulation : Wrapping up of data and code into a single unit is called
Encapsulation.
Abstraction : hide the internal details and show the essential details of the
object is called as Abstraction or data hiding in java .
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
YouTube : Android & Java Tutorial
Comments
Post a Comment