Java- Object Oriented Programming Tutorial In Java (oops concept) - JavaTechWorld

Monday, 30 January 2017

Java- Object Oriented Programming Tutorial In Java (oops concept)

Java supports the following oops concepts −Classes
  • Object
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction
Classes - It is bluepring or template of an object.
Ex -
  1. public class Car
  2. {
  3. String Gear;
  4. String color;
  5. static String Company="Tata";
  6. void forward()
  7. {
  8. int test;
  9. }
  10. void reverse()
  11. {
  12. }
  13. void neutral ()
  14. {
  15. }
  16. }
A class can contain any of the following variable types.
  • Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. Here test variable inside forward method is local variable.
  • Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. Here Gear, Color are instance variable.
  • Class variables − Class variables are variables declared within a class, outside any method, with the static keyword. Here Company is the Class variable.
Object - It is encapsulated form of static and non-static method and variable.
Inheritance -In oops Inheritace is the process of acessing the properties of parent class in our subclass is known as Inheritance.
The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).
To implement inheritance we have to use extends and implements keyword.
Ex -
  • class Super
  • {
    ......
  • }
  • class Sub extends Super
  • {
    ......
  • }
Polymorphism- More than one method with same name but different signature defined in the same class or with same signature defined in the different class is called as polymorphism.
Following oops concept demonstrate different types of polymorphism.
  • Method Overloading
  • Method Overriding
Method Overloading- More than one method with same name and with same signature defined in the same class is called as Method Overloading.
Ex-
class A {
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;
}
}
class MethodOverloading
{
public static void main (String args [])
{
A Obj = new A();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
}
}
Here the method demo() is overloaded 3 times: first having 1 int parameter, second one has 2 int parameters and third one is having double arg. The methods are invoked or called with the same type and number of parameters used.
Rules of Method Overloading- 
  • Overloading can take place in the same class or in its sub-class.
  • Constructor in Java can be overloaded
  • Overloaded methods must have a different argument list.
  • The parameters may differ in their type or number, or in both.
  • They may have the same or different return types.
  • It is also known as compile time polymorphism.
Why to use Method Overloading in oops-
In the above example you have same function demo but you want to perform some operation using different data so we have created a same function demo but with different parameter.
Method Overriding in oops-
More than one method with same name and same signature but defined in the different class is called as Method Overriding.
Rules of Method Overriding-
  • applies only to inherited methods
  • object type (NOT reference variable type) determines which overridden method will be used at runtime
  • Overriding method can have different return type (refer this)
  • Overriding method must not have more restrictive access modifier
  • Abstract methods must be overridden
  • Static and final methods cannot be overridden
  • Constructors cannot be overridden
  • It is also known as Runtime polymorphism.
public class Base
{
public void methodToOverride()      //Base class method
{
System.out.println ("I'm the method of BaseClass");
}
}
public class Derived extends Base
{
public void methodToOverride()     //Derived Class method
{
System.out.println ("I'm the method of DerivedClass");
}
}
public class TestMethod
{
public static void main (String args []) {
// Base class reference and object
Base obj1 = new Base();
// Base class reference but Derived Class object
Base obj2 = new Derived();
// Calls the method from Base class class
obj1.methodToOverride();
//Calls the method from Derived class class
obj2.methodToOverride();
}
}
Why to use Method Overriding in oops-
Overriding is useful when you want to change the functionality of some method based on a class.
Encapsulation in oops- Encapsulation is the process of hiding the implementation details from the outside world and accessing through publicly exposed method that means setter or getter method.
Ex-
public class EnDemo{
private int sno;
private String empName;
private int empAge;
//Getter and Setter methods
public int getEmpSNO(){
return sno;
}
public String getEmpName(){
return empName;
}
public int getEmpAge(){
return empAge;
}
public void setEmpAge(int newValue){
empAge = newValue;
}
public void setEmpName(String newValue){
empName = newValue;
}
public void setEmpSNO(int newValue){
sno = newValue;
}
}
public class EncapsTest{
public static void main(String args[]){
EnDemo obj = new EnDemo();
obj.setEmpName("Mario");
obj.setEmpAge(32);
obj.setEmpSNO(112233);
System.out.println("Employee Name: " + obj.getEmpName());
System.out.println("Employee SNO: " + obj.getEmpSNO());
System.out.println("Employee Age: " + obj.getEmpAge());
}
}
In above example all the three data members (or data fields) are private which cannot be accessed directly. These fields can be accessed via public methods only. Fields empName, sno and empAge are made hidden data fields using encapsulation technique of OOPs.
Abstraction in oops- Abstraction is the process of hiding implementation details and providing functionality to the user, in other word we can say show functionality and hide complexity.Abstraction can be implemented using abstract class and interface.
Ex-
abstract class Base
{
int x,y;
void show()
{
System.out.println("x");
System.out.println("y");
}
abstract void display();
}
class Derived extends Base
{
void set (int x,int y)
{
this.x=x;
this.y=y;
System.out.println("x "+x);
System.out.println("y "+y);
}
void display()
{
System.out.println("revoke");
}
public static void main(String args[])
{
Derived d1= new Derived();
d1.set(10,20);
d1.show();
d1.display();
}
}

© 2014 JavaTechWorld. Designed by Bloggertheme9
Powered by Blogger.