Simple Samples of Java OOP 4 Principles

Weijing Jay Lin
1 min readFeb 21, 2022

--

Inheritance

Basically it means a class extends to another class for inheritance.

class A {}
class B extends B {}

In Java, it also support multiple inheritance.

class A {}
class B {}
class C extends A, B {}

Polymorphism

Basically it is concept of overriding, overloading and assignment for a class.

class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}

class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}

class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}

Abstraction

We can create an abstract class without filling the detail of the function, eg abstract class or interface.

// abstract class
public abstract class A { public abstract void abstractMethod(); }
// interface I
public interface I { public void interfaceMethod(); }

Encapsulation

It is a permission control level.

// you can only access parameter "name" if you use getName() method.
public class E {
private String name;
public String getName {
// passing the auth check
return name;
}
}

Check here for more on Java cheat sheet:

--

--

Weijing Jay Lin
Weijing Jay Lin

Written by Weijing Jay Lin

A software engineer entrepreneur in San Francisco Bay Area.

No responses yet