Java objects examples

Alexandra 32 Published: 08/06/2024

Java objects examples

Here are some examples of using Java objects:

Creating an Instance of a Class:
public class Person {

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

public void sayHello() {

System.out.println("Hello my name is " + name);

}

}

public static void main(String[] args) {

Person person = new Person("John", 30);

person.sayHello(); // Output: Hello my name is John

}

Encapsulating Data:
public class BankAccount {

private double balance;

public void deposit(double amount) {

balance += amount;

}

public void withdraw(double amount) {

if (balance >= amount) {

balance -= amount;

} else {

System.out.println("Insufficient funds");

}

}

public double getBalance() {

return balance;

}

}

public static void main(String[] args) {

BankAccount account = new BankAccount();

account.deposit(1000.00);

account.withdraw(500.00); // Output: Insufficient funds

}

Inheritance:
public class Animal {

public void eat() {

System.out.println("Eating...");

}

}

public class Dog extends Animal {

@Override

public void eat() {

System.out.println("Dog is eating dog food");

}

}

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.eat(); // Output: Dog is eating dog food

}

Polymorphism:
public class Shape {

public abstract void draw();

}

public class Circle extends Shape {

@Override

public void draw() {

System.out.println("Drawing a circle");

}

}

public class Rectangle extends Shape {

@Override

public void draw() {

System.out.println("Drawing a rectangle");

}

}

public static void main(String[] args) {

Shape shape = new Circle();

shape.draw(); // Output: Drawing a circle

shape = new Rectangle();

shape.draw(); // Output: Drawing a rectangle

}

Composition:
public class Car {

private Engine engine;

public Car(Engine engine) {

this.engine = engine;

}

public void start() {

engine.start();

}

}

public class ElectricEngine implements Engine {

@Override

public void start() {

System.out.println("Electric car started");

}

}

public static void main(String[] args) {

Car myCar = new Car(new ElectricEngine());

myCar.start(); // Output: Electric car started

}

Static Methods and Variables:
public class MathUtils {

public static int add(int a, int b) {

return a + b;

}

}

public static void main(String[] args) {

int result = MathUtils.add(2, 3); // Output: 5

}

Abstract Classes and Interfaces:
public abstract class AnimalSound {

public abstract void makeSound();

}

public interface Flyable {

void fly();

}

public class Dog extends AnimalSound implements Flyable {

@Override

public void makeSound() {

System.out.println("Dog is barking");

}

@Override

public void fly() {

System.out.println("Dogs cannot fly");

}

}

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.makeSound(); // Output: Dog is barking

myDog.fly(); // Output: Dogs cannot fly

}

Nested Classes:
public class OuterClass {

public class InnerClass {

public void doSomething() {

System.out.println("Doing something");

}

}

public static void main(String[] args) {

OuterClass outer = new OuterClass();

OuterClass.InnerClass inner = outer.new InnerClass();

inner.doSomething(); // Output: Doing something

}

}

Generics:
public class Box {

private T t;

public void set(T t) {

this.t = t;

}

public T get() {

return t;

}

}

public static void main(String[] args) {

Box box = new Box<>();

box.set("Hello");

String result = box.get(); // Output: Hello

}

Lambda Expressions:
public class Person {

private String name;

public Person(String name) {

this.name = name;

}

public void sayHello() {

System.out.println("Hello my name is " + name);

}

}

public static void main(String[] args) {

List people = Arrays.asList(new Person("John"), new Person("Mary"));

people.forEach(p -> p.sayHello());

}

These are just a few examples of how Java objects can be used. There are many more features and capabilities in Java that are not listed here.

What is an object in Java for dummies?

I'd be happy to explain what an object in Java is, but I'll have to do it in English since you asked nicely.

In Java, an object is a representation of a real-world entity or concept that has properties and behaviors. Think of it like a virtual LEGO brick - just as a LEGO brick can have different shapes, colors, and sizes, an object in Java can have different characteristics, such as its shape (its structure) and color (its behavior).

In simple terms, an object is an instance of a class that has:

State: Properties or attributes that describe the object's characteristics, like size, color, or shape. Behavior: Methods or actions that the object can perform, like moving, speaking, or calculating.

To create an object in Java, you need to define a class for it using the class keyword. The class is essentially a blueprint or a template for creating objects. When you create an instance of a class (using the new keyword), you're actually creating an object with its own unique set of characteristics.

Here's an example:

Let's say we want to create a simple "Student" object in Java. We might define a class like this:

public class Student {

private String name;

private int age;

public Student(String name, int age) {

this.name = name;

this.age = age;

}

public void speak() {

System.out.println("Hello! My name is " + name);

}

}

In this example:

The Student class has two properties (state): name and age. The Student class has one behavior (method): speak, which prints out a message.

Now, let's create an instance of the Student class:

Student student1 = new Student("John", 25);

student1.speak(); // Output: Hello! My name is John

In this example:

We created an object called student1, which has its own unique values for name and age. We can call the speak method on the student1 object, which prints out a message with the student's name.

That's basically it - objects in Java are instances of classes that have their own set of properties (state) and behaviors (methods). This concept is crucial to understanding how Java programs work, as it allows you to create reusable code that can represent complex real-world concepts.