My journey to Java 004 — OOP concepts

Encapsulation

Encapsulation is hiding data away from the end user i.e, If we have a password variable and we need to keep it private so only the class and it’s members will be able to access it, Then we made an encapsulation as we hide the password variable inside the class. But what if we need to set or get this variable, In this case, we are going to create setPassword and getPassword methods which could be used by the user outside the class ( public) methods.

package JourneyToJava004;

class encapsulation {
private String password = "";

public void setPassword(String password_value){
password = password_value;
}

public String getPassword(){
return password;
}
}


public class encapsulationTest{
public static void main(String args[]) {
encapsulation obj = new encapsulation();
obj.setPassword("0xIslamTaha");
System.out.println(obj.getPassword());
}

}

Inheritance

One of the most important OOP concepts and to understand that let we say we wanna talk about the animals. All of them need to eat and drink but some of them walk by 2 legs and others walk by 4 legs so If I asked you to create a class and represent those 3 methods (eat, drink and walk) the simple solution creates two class each of them has those 3 methods but we would have duplicate code as we would write eating and drinking methods twice! and this isn’t smart enough so here inheritance comes to solve this problem. We will create a parent class which has all common methods i.e. eating and drinking methods then we create two classes as child classes each of them inhert from the parent class and has its own walking method. hmm but Hi! You don’t tell us what inheritance mean till now! :D

package JourneyToJava004;

class parent{
public void eating(String animal){
System.out.println(String.format("%s animal is eating", animal));
}

public void drinking(String animal){
System.out.println(String.format("%s animal is drinking", animal));
}
}

class childTwoLegs extends parent{
public static String animalType = "twoLegs";

public void twoLegs(){
System.out.println("Animal with two legs");
}
}

class childFourLegs extends parent{
public static String animalType = "fourLegs";

public void fourLegs(){
System.out.println("Animal with four legs");
}
}

public class InheritanceTest{
public static void main(String args[]) {
childTwoLegs obj_2_legs = new childTwoLegs();
obj_2_legs.twoLegs();
obj_2_legs.eating(obj_2_legs.animalType);
obj_2_legs.drinking(obj_2_legs.animalType);

childFourLegs obj_4_legs = new childFourLegs();
obj_4_legs.fourLegs();
obj_4_legs.eating(obj_4_legs.animalType);
obj_2_legs.drinking(obj_4_legs.animalType);
}
}
/* OUTPUT
Animal with two legs
twoLegs animal is eating
twoLegs animal is drinking
Animal with four legs
fourLegs animal is eating
fourLegs animal is drinking */

Polymorphism

Polymorphism allows us to perform a single action in different ways. Lets say that in the parent class we defined sound method but there is a unique sound for each animal then we are going to override it in each child class. hmm?! What is overrideing ?!

class Parent{
public void sound(){
System.out.println("Generic sound method");
}
}
class CatChild extends Parent{
@Override
public void sound(){
System.out.println("Meow Meow");
}
}
class DogChild extends Parent{
@Override
public void sound(){
System.out.println("Haoo Haoo");
}
}

public class polymorphismTest {
public static void main(String args []){
CatChild catObj = new CatChild();
catObj.sound();

DogChild dogObj = new DogChild();
dogObj.sound();
}
}
/* OUTPUT
Meow Meow
Haoo Haoo
*/

Interfaces

As a definition, Interface looks like a class which has methods’ declarations but not a body … hmm .. but where the methods’ body will be?! We will implement it in another class. Let’s see an example

interface Interface_ex {
public void implementME();
}
class implementation implements Interface_ex{
@Override
public void implementME() {
System.out.println("Here is my body!");
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store