Object-Oriented Programming system in Java

  1. Polymorphism
  2. Abstraction
  3. Inheritance
  4. Encapsulation

OOPs features

public class Calc{
public int sum(int a, int b){
return a + b;
}
public double sum(int a, double b){ //overloading method
return a + b;
}
public double sum(double a, int b){ //overloading metod
return a + b;
public double sum(double a, double b){ //overloading method
return a + b;
}
}
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog { public static void main(String args[]) {

Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}
Animals can move
Dogs can walk and run

c
|
---------------
↑ ↑
| |
A B

|
D

Example


public class EncapTest {
private String name;
private String idNum;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public void setAge( int newAge) {
age = newAge;
}
public void setName(String newName) {
name = newName;
}
public void setIdNum( String newId) {
idNum = newId;
}
}

Conclusion

--

--

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