Core Java Tutorial OOPS Concepts with Examples

By | January 29, 2012
This tutorial will help you to understand about Java OOPS concepts with examples. Java is a Object Oriented Programming Language (OOPS). Lets we discuss about what are the features of OOPS. Object Oriented Approach is the concept of number of objects interact with each others. It is a programming methodology to design computer programs using classes and objects.

There are four main features of OOPS.
1) Encapsulation
2) Inheritance
3) Polymorphism
4) Abstraction
Lets we discuss about the about features in details.

Encapsulation
Encapsulation means putting together all the variables (Objects) and the methods into a single unit called Class. So define a user defined data type called the keyword Class followed by the class name and inside the class we need the specify the variables that is also called as attributes and methods.

Example :
Class EncaptulationExample {
int obj;
char cObj;
public void javaMethod(){
}
}

Inheritance
Inheritance is mainly used for code reusability. You already have defined an Object or rather you have already defined set of attributes and characteristics which you like to make you it again and expand up on it. So you are making use of already written class and further extending on that. That why we discussed about the code reusability the concept. In general one line definition we can tell that deriving a new class from existing class, it’s called as Inheritance. You can look into the following example for inheritance concept.
Example 1:
class StudentInfo {
String name;
int rollno;
int get(String n, int r){
name=n; rollno=r; return(0);
}
void showDetails(){
System.out.println(“Name : “+name);
}
}

class InheritanceExampleDemo extends StudentInfo{
public static void main(String args[]){
StudentInfo studObj = new StudentInfo();
studObj.get(“Eswar”,92);
studObj.showDetails();
}
void displayDetails(){
System.out.println(“Sample Info Display”);
}
}
Output:
Name : Eswar

Polymorphism:

In Core Java Polymorphism is one of easy concept to understand. Polymorphism definition is that Poly  means Many morphos means froms . Its refer to the objects ability to active Polymorphism depends on its type.

There are two types of Polymorphism available in Java.

1)Static Polymorphism

2) Dynamic Polymorphism

Let’s we discuss about Static Polymorphism, Its Compile time Polymorphism. Method overloading is the concept of two or more methods in a Java Class can have same name and it their arguments lists are different. We also have another two important concepts in Polymorphism, Method Overloading and Method Overridding. The following example program will make you understand the Method Overloading.

Example for Method overloading
class Subjects {
void add(int tamil, int english){
System.out.println(“The total of tamil and english is “+(tamil+english));
}

void add(int tamil,int english,int maths){
System.out.println(“The total of tamil english and maths is “+(tamil+english+maths));
}
}

public class MethodOverloadingDemo{
public static void main(String arg[]){
//create Subjects class object
Subjects sb=new Subjects();
// we have to call add() method by passing 2 values
sb.add(90, 80);
//here also we are calling add() method by passing 3 values, So the 3 arguments (parameters) method will get execute.
sb.add(95,85,100);
}
}
Output for the above program is :
The total of tamil and english is 170
The total of tamil english and maths is 280

Now we will discuss about what is dynamic polymorphism, Its run time polymorphism. We can also call it as Method overridding. Method overridding is the concept of two or more method, constructor have a same name in super and sub class with same signature. This feature is called method overridding.

Method Overloading Example Program:
class MathsSquareDemo1 {
void calculate(double price){
System.out.println(“Sqare value “+(price*price));
}

}

class MathsSquareDemo2 extends MathsSquareDemo1 {
void calculate(double price){
System.out.println(“Sqare value “+(Math.sqrt(price)));
}
}

public class MethodOverriddingDemo{
public static void main(String arg[]){
MathsSquareDemo2 msd=new MathsSquareDemo2();
msd.calculate(25);
}
}

Output :

Sqare value 5.0

Abstraction

Process of exploring relevant details and hiding irrelevant details this feature is known as Abstraction. In other way making simplicity to use complex system. One does not want to understand how to engine works. Similarly one does not have to understand the internal implementation of the software objects.

Abstraction Example : Engine, Driving.

Abstraction main advantage is that every user will get data according to their exact requirement. User will not get confused with unnecessary data. The following example program will make you to understand Abstraction.

Java Abstraction Example Program:

public class AbstractionDemo {
private int accountNo;
private String customerName;
private float accountBalance;
private float profit;
private float loan;

public void dislayClerkInfo(){
System.out.println(“Accout number “+accountNo);
System.out.println(“Customer name “+customerName);
System.out.println(“Account Balance “+accountBalance);
}

}

In the above program clerk can access only the limited details like account number, customer name and account balance. User cannot access other details like profit and loan.

No related content found.