My journey to Java 004 — OOP concepts
Hello everybody. Hope all of you are doing well. Last time we talked a little bit about the collections. Today we will talk about OOP concepts like encapsulation, inheritance, polymorphism, and interfaces.
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());
}
}
Here we created an encapsulation
class and defined a private
variable then we created a public
methods to set
and get
the value.
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
Inheritance means we have two class parent
and child
classes and the parent class have all the common methods while the child class will inherit all those methods plus his own methods
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 */
As you can see here we created three class one as a parent class and two child classes each one of the child class extends
the parent class (for some reason java guys use extends
word to do the inheritance
job) then we created an object for each child and as u can see here we can call eating
method from the child object.
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
?!
Overriding
overriding means that we provide an implementation for the parent generic method inside the child class i.e if the parent
class has a generic sound
method, the child
class will have another implementation of the same method and the child’s object will call this new implementation.
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();
}
and here is the way of implementation
class implementation implements Interface_ex{
@Override
public void implementME() {
System.out.println("Here is my body!");
}
}
As a note, we can only inhert
one class however we can extend
any number of interfaces.
so we discussed encapsulation, inheritance, polymorphism and interfaces concepts and you can find the code here. Keep in touch there is more ….