My journey to Java 004 — OOP concepts

Encapsulation

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

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

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

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

--

--

--

code with love!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Vault: Dynamic DB Credentials

FlexBoxing

Network Topology Setup to connect Google but Not Facebook

Embed code not available

SberDevices презентовала умную медиаколонку SberBox Time

Istio in GKE [Part 4] Distributed Tracing using Jaeger [Basic]

How to produce a Web-site on your own for free?

How to start local devnet Filecoin node and miner for development purposes

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
Islam Taha

Islam Taha

code with love!

More from Medium

Java Concepts — Object Oriented Programming I

02. A Trip to Objectville

Basics of Java Programming

A better guide to learn Object Oriented Programming

A glowing bulb