Back to the page

In the print window, choose “Save as PDF”.

doubtly doubtly.in

Introduction to Object-Oriented Programming (OOP) – Module 01 Java SBLC

Java · Updated 25 Jul 2024 · Read online: www.doubtly.in/introduction-to-object-oriented-programming-oop-module-01-java-sblc/

Introduction to Object-Oriented Programming (OOP)

1.1 OOP Concepts:

  public class Car {
      String color;
      String model;
      int year;

      void drive() {
          System.out.println("The car is driving.");
      }

      void brake() {
          System.out.println("The car is braking.");
      }
  }
  public class Car {
      private String color;
      private String model;
      private int year;

      public String getColor() {
          return color;
      }

      public void setColor(String color) {
          this.color = color;
      }
  }
  public class Vehicle {
      String brand;
      void honk() {
          System.out.println("Honk! Honk!");
      }
  }

  public class Car extends Vehicle {
      String modelName = "Mustang";
  }
  class Animal {
      void sound() {
          System.out.println("Animal makes a sound");
      }
  }

  class Dog extends Animal {
      void sound() {
          System.out.println("Dog barks");
      }
  }

  class Cat extends Animal {
      void sound() {
          System.out.println("Cat meows");
      }
  }

  public class TestPolymorphism {
      public static void main(String[] args) {
          Animal a;
          a = new Dog();
          a.sound();
          a = new Cat();
          a.sound();
      }
  }
  public class Sender {
      public void sendMessage() {
          System.out.println("Message sent!");
      }
  }

  public class Receiver {
      public static void main(String[] args) {
          Sender sender = new Sender();
          sender.sendMessage();
      }
  }

1.2 Java Virtual Machine (JVM):

1.3 Basic Programming Constructs:

  int age = 25;
  String name = "Ajink";
  int a = -1;
  System.out.println(a >>> 1); // Outputs 2147483647
  int result = 5 + 3;
  int number = 10;
  if (number > 0) {
      System.out.println("Positive number");
  } else {
      System.out.println("Negative number");
  }
  for (int i = 0; i < 5; i++) {
      System.out.println(i);
  }

Practise this on Doubtly (free)