public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Let me explain the different parts of this program:
public class HelloWorld
: This line defines a class namedHelloWorld
. In Java, every application begins with at least one class, and the name of the class must match the filename (in this case,HelloWorld.java
).public static void main(String[] args)
: This line defines themain
method, which is the entry point of the Java program. Themain
method is where the program starts executing. It takes an array of strings (args
) as input parameters.System.out.println("Hello, World!");
: This line prints the string "Hello, World!" to the console. In Java,System.out
refers to the standard output stream, andprintln
is a method used to print a string followed by a newline character.
To run this program:
- Open a text editor and copy the above code into a new file named
HelloWorld.java
. - Save the file.
- Open a command prompt or terminal.
- Navigate to the directory where you saved
HelloWorld.java
. - Compile the program by running
javac HelloWorld.java
. - If there are no errors, run the program by typing
java HelloWorld
. - You should see the output
Hello, World!
printed to the console.
Explanation:
- We define a class named
Car
with three instance variables:brand
,model
, andyear
. - The class has a constructor
Car(String brand, String model, int year)
to initialize the instance variables when aCar
object is created. - It also has a method
displayInfo()
to display information about the car. - In the
Main
class, we create two instances of theCar
class (car1
andcar2
) and initialize them with different values. - We then call the
displayInfo()
method on each car object to print their information.
This program demonstrates encapsulation (by making instance variables private
), constructor overloading (the Car
class has one constructor), and method invocation (calling the displayInfo()
method on car objects). These are core concepts of object-oriented programming in Java.
Here's another example that expands on the previous one by adding inheritance and polymorphism:
// Define a superclass named Vehicle class Vehicle { // Instance variables private String brand; private String model; private int year; // Constructor public Vehicle(String brand, String model, int year) { this.brand = brand; this.model = model; this.year = year; } // Method to display vehicle information public void displayInfo() { System.out.println("Brand: " + brand); System.out.println("Model: " + model); System.out.println("Year: " + year); } } // Define a subclass named Car that extends Vehicle class Car extends Vehicle { // Additional instance variable private int numOfDoors; // Constructor public Car(String brand, String model, int year, int numOfDoors) { super(brand, model, year); // Call the superclass constructor this.numOfDoors = numOfDoors; } // Override the displayInfo method @Override public void displayInfo() { super.displayInfo(); // Call the superclass method System.out.println("Number of doors: " + numOfDoors); } } public class Main { public static void main(String[] args) { // Create an instance of Car Car car = new Car("Toyota", "Camry", 2020, 4); // Display information about the car car.displayInfo(); } }
Explanation:
- We define a superclass named
Vehicle
with instance variablesbrand
,model
, andyear
, and a methoddisplayInfo()
to display vehicle information. - We define a subclass named
Car
that extendsVehicle
and adds an additional instance variablenumOfDoors
. - The
Car
class has its own constructor that initializes its instance variables and overrides thedisplayInfo()
method to include information about the number of doors. - In the
Main
class, we create an instance ofCar
and call itsdisplayInfo()
method. This demonstrates polymorphism, as thedisplayInfo()
method of theCar
class overrides the method of the superclassVehicle
.
Here's another example demonstrating inheritance and polymorphism in Java:
// Define a superclass named Animal class Animal { // Method to make sound public void makeSound() { System.out.println("Some generic sound"); } } // Define a subclass named Dog that extends Animal class Dog extends Animal { // Override the makeSound method @Override public void makeSound() { System.out.println("Woof woof!"); } } // Define a subclass named Cat that extends Animal class Cat extends Animal { // Override the makeSound method @Override public void makeSound() { System.out.println("Meow meow!"); } } public class Main { public static void main(String[] args) { // Create instances of Dog and Cat Animal dog = new Dog(); Animal cat = new Cat(); // Call the makeSound method for each animal dog.makeSound(); // Output: Woof woof! cat.makeSound(); // Output: Meow meow! } }
Explanation:
- We define a superclass named
Animal
with a methodmakeSound()
that prints a generic sound. - We define subclasses
Dog
andCat
that extendAnimal
and override themakeSound()
method with specific sounds for each animal. - In the
Main
class, we create instances ofDog
andCat
, but store them in variables of typeAnimal
. This demonstrates polymorphism, as we can treat objects of subclasses as objects of the superclass. - When we call the
makeSound()
method for each animal, the overridden version of the method in the respective subclass is executed, demonstrating dynamic method dispatch.
In Java, the term "main method" typically refers to the entry point of a Java program, which is a special method named main
. This method has a specific signature, which allows the Java Virtual Machine (JVM) to execute the program. Here's a brief explanation of the main method and how it differs from normal methods:
Main Method:
- The
main
method is the starting point of a Java program. It has the following signature:
- The
public static void main(String[] args)
- It must be declared as
public
, so it can be accessed by the JVM. - It must be declared as
static
, meaning it belongs to the class itself rather than any specific instance of the class. - It returns
void
, indicating that it does not return any value. - It takes a single parameter of type
String
array (String[] args
), which allows you to pass command-line arguments to the program. - The JVM looks for the
main
method with this specific signature to start executing the Java program.
- It must be declared as
Normal Methods:
- Normal methods in Java are regular functions defined within a class that perform specific tasks.
- They can have any name that adheres to Java's identifier naming rules.
- They can have various access modifiers (e.g.,
public
,private
,protected
, or package-private) to control their visibility and accessibility. - They can have any return type (except
void
for constructors). - They can take zero or more parameters of any data type.
- They are invoked by calling the method name on an object or class instance.
In summary, the main method is a special method that serves as the entry point of a Java program, while normal methods are regular functions used to perform specific tasks within a class.
0 Comments