javascript
Brief description  about Online courses   join in Online courses
OR

Object Oriented Programming Concepts In Java

Shatish  Rao
Shatish Rao
Project Lead - Java

Java is a Object Oriented Programming Language (OOPS). Lets we discuss about what are the features of OOPS. 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:
Polymorphism is an Oops concept which advice use of common interface instead of concrete implementation while writing code. When we program for interface our code is capable of handling any new requirement or enhancement arise in near future due to new implementation of our common interface. If we don't use common interface and rely on concrete implementation, we always need to change and duplicate most of our code to support new implementation. Its not only java but other object oriented language like C++ also supports polymorphism and it comes as fundamental along with encapsulation , abstraction and inheritance.

class TradingSystem{
public String getDescription(){
return "electronic trading system";
}
}

class DirectMarketAccessSystem extends TradingSystem{
public String getDescription(){
return "direct market access system";
}
}

class CommodityTradingSystem extends TradingSystem{
public String getDescription(){
return "Futures trading system";
}
}

Abstraction
something which is not concrete , something which is imcomplete.

java has concept of abstract classes , abstract method but a variable can not be abstract.

an abstract class is something which is incomplete and you can not create instance of it for using it.if you want to use it you need to make it complete by extending it.

an abstract method in java doesn't have body , its just a declaration.

so when do you use abstraction ?
when I know something needs to be there but I am not sure how exactly it should look like.

e.g. when I am creating a class called Vehicle, I know there should be methods like start() and Stop() but don't know start and stop mechanism of every vehicle since they could have different start and stop mechanism e..g some can be started by kick or some can be by pressing buttons

Write your comment now