java object oriented programming code example

Example 1: java oop

public class YourClass {
  String example;
  int test;
  
  // Constructor
  public YourClass(String example, int test) {
    this.example = example;
    this.test = test;
  }
  
  // Method
  public void someMethod() {
    System.out.println(example); 
  }
}

// Usage:
// Construct
YourClass exampleObject = new YourClass("Hello World!", 5);
// Execute Method:
exampleObject.someMethod();

Example 2: object orientation in java

class Person {
 void walk() {
  System.out.println(Can Run.);
 }
}
class Employee extends Person {
 void walk() {
  System.out.println(Running Fast…”);
 }
 public static void main(String arg[]) {
  Person p = new Employee(); //upcasting
  p.walk();
 }
}

Tags:

Java Example