Java object oriented programming examples w3schools

Karen 125 Published: 11/23/2024

Java object oriented programming examples w3schools

I'll respond with English-only content. Here are some Java object-oriented programming examples from W3Schools, along with explanations and additional details:

Example 1: Animal Class

public class Animal {

private String name;

private int age;

public Animal(String name, int age) {

this.name = name;

this.age = age;

}

public void eat() {

System.out.println(name + " is eating.");

}

}

This example demonstrates a basic animal class with two properties: name and age. The constructor initializes these values. The eat() method simulates the animal's behavior of eating.

Example 2: Dog Class Inheritance

public class Animal {

private String name;

private int age;

public Animal(String name, int age) {

this.name = name;

this.age = age;

}

public void eat() {

System.out.println(name + " is eating.");

}

}

public class Dog extends Animal {

public Dog(String name, int age) {

super(name, age);

}

public void bark() {

System.out.println("Woof!");

}

}

This example showcases inheritance in Java. The Dog class inherits properties and methods from the Animal class using the extends keyword. It also adds a new method called bark() specific to dogs.

Example 3: Bank Account Class

public class BankAccount {

private String accountNumber;

private double balance;

public BankAccount(String accountNumber, double initialBalance) {

this.accountNumber = accountNumber;

this.balance = initialBalance;

}

public void deposit(double amount) {

balance += amount;

System.out.println("Deposit successful. New balance: " + balance);

}

}

This example illustrates a BankAccount class with two properties: accountNumber and balance. The constructor sets the initial balance, and the deposit() method simulates depositing money into the account.

Example 4: Shape Class Polymorphism

public abstract class Shape {

public abstract double getArea();

}

public class Rectangle extends Shape {

private double width;

private double height;

public Rectangle(double width, double height) {

this.width = width;

this.height = height;

}

public double getArea() {

return width * height;

}

}

public class Circle extends Shape {

private double radius;

public Circle(double radius) {

this.radius = radius;

}

public double getArea() {

return Math.PI * radius * radius;

}

}

This example demonstrates polymorphism in Java. The Shape abstract class has an abstract method called getArea(). The Rectangle and Circle classes inherit from the Shape class and override the getArea() method to provide their specific calculations.

These examples showcase various aspects of object-oriented programming (OOP) in Java, including encapsulation, inheritance, polymorphism, and composition. W3Schools provides additional explanations and tutorials for each example, allowing you to learn more about these concepts and how they can be applied in real-world scenarios.

How to write an OOPs program in Java?

I'll respond in English this time.

Object-Oriented Programming (OOP) is a programming paradigm that uses concepts like classes, objects, inheritance, polymorphism, and encapsulation to develop software systems. In Java, you can write an OOP program by following these steps:

Step 1: Define Classes

In Java, a class is a blueprint for creating objects. A class defines the properties (data) and behaviors (methods) of an object. For example, you could create a Student class with properties like name, age, and grade, and methods like study() and play(). To define a class in Java, use the public class keyword followed by the name of the class.

Step 2: Create Objects

Once you've defined a class, you can create objects (instances) of that class. For example, you could create multiple Student objects each with its own unique name, age, and grade. To create an object in Java, use the new keyword followed by the name of the class.

Step 3: Use Constructors

Constructors are special methods that are called when an object is created. They're used to initialize the properties (data) of an object. For example, you could have a constructor for the Student class that takes name, age, and grade as parameters and sets them accordingly.

Step 4: Use Methods

Methods are blocks of code that perform specific actions on objects. In Java, methods can be used to manipulate data, perform calculations, or interact with other objects. For example, you could have a study() method in the Student class that increments a studyTime variable.

Step 5: Use Inheritance

Inheritance is a mechanism in OOP where one class (the child) inherits properties and behaviors from another class (the parent). This allows for code reuse and facilitates the creation of hierarchical relationships between classes. For example, you could have a UniversityStudent class that inherits from the Student class.

Step 6: Use Polymorphism

Polymorphism is the ability of an object to take on multiple forms. In Java, polymorphism can be achieved through method overriding or method overloading. Method overriding involves creating a new method with the same signature as a parent class's method but with different implementation. Method overloading involves creating multiple methods with the same name but different parameter lists.

Step 7: Use Encapsulation

Encapsulation is the mechanism by which objects hide their internal data and behavior from other parts of the program, while exposing only necessary information through public interfaces (methods). In Java, encapsulation can be achieved through the use of private fields and public getter/setter methods.

Here's an example of how you could implement these concepts in a simple Student class:

public class Student {

private String name;

private int age;

private int grade;

// Constructor

public Student(String name, int age, int grade) {

this.name = name;

this.age = age;

this.grade = grade;

}

// Method to study

public void study() {

System.out.println(name + " is studying...");

}

// Getter and setter methods for private fields

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

This Student class has a constructor that initializes the name, age, and grade properties. It also has a study() method that prints a message to the console, as well as getter and setter methods for accessing and modifying the private fields.

I hope this helps! Let me know if you have any questions or need further clarification.