Basic Structure Of
Java Program
// 1. Package Declaration (optional)
package mypackage;
// 2. Import Statements (optional)
import java.util.Scanner;
// 3. Class Declaration
public class MyProgram {
// 4. Main Method – Entry point of the program
public static void main(String[] args) {
// 5. Statements – Code to be executed
System.out.println("Hello, World!");
}
}
1. Package Declaration
(Optional)
A Java program may begin with a package declaration that defines the
folder or package name in which the class is stored.
For example: package
mypackage;
This helps organize code into different logical groups.
2. Import Statements (Optional)
After the package declaration, import statements can be added to include
built-in or user-defined Java classes and packages.
For example: import
java.util.Scanner;
This allows the program to use various functionalities provided by external
classes.
3. Class Declaration
Every Java program must have at least one class, which is the main building block of
Java code.
For example: public
class MyProgram { }
The class name should match the filename, and it contains all the methods and
variables of the program.
4. Main Method Declaration
Inside the class, the main method is defined using the line:
public
static void main(String[] args)
This is the entry point of every Java application, and the JVM starts executing
the program from this method.
5. Statements (Code Body)
Inside the main method, we write statements that define the program's behavior,
such as printing output, taking input, or performing calculations.
For example: System.out.println("Hello,
World!");
These statements are executed in the order they appear.
Features Of OOP
Object
Objects are the entities through which we perceive the world
around us. We naturally see our environment as being composed of things which
have recognizable identities and behavior. The entities are then represented as
objects in the program. In OOP system, an
object is the run time entity i.e. a region of storage with
associated semantics. In OOP "Object" usually means an instance of a
class.
Example of Class and Object
public class Main //Main is name of class
{
int num = 15;
public static void main(String[] args) {
Main myObj1 = new Main();
Main myObj2 = new Main();
// myObj1 and myObj2 are object
of main class
System.out.println(myObj1.num);
System.out.println(myObj2.num);
}
}
Abstraction
Abstraction means showing only the essential
details and hiding the unnecessary parts.
It helps reduce complexity by focusing only on what an object does, not how it
does it.
Real-life example: When you use a TV
remote, you press buttons to control it, but you don't need to know how it
works inside.
Java Program
Example – Abstraction using Abstract Class:
abstract class Animal {
abstract void
sound(); // abstract method (no body)
}
class Dog extends Animal {
public void
sound() {
System.out.println("Bark");
}
}
public class Main {
public static
void main(String[] args) {
Animal d = new
Dog();
d.sound(); // Output: Bark
}
}
Explanation: The user only knows the sound() method works, but doesn't care about how it is implemented inside Dog.
Inheritance
Inheritance means a class (called child or subclass)
can get the properties and methods of another class (called parent or superclass).
It helps in code reuse and allows you to create new classes based on
existing ones.
🔸 Real-life
example: A son inherits features from his father.
Java Program
Example – Inheritance:
class Animal {
void eat() {
System.out.println("This animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static
void main(String[] args) {
Dog d = new
Dog();
d.eat(); // Inherited from Animal
d.bark(); // Own method
}
}
Explanation: The Dog class can use
both eat() and bark() methods, even though it only defines bark().
Encapsulation
Encapsulation means keeping the data (variables) safe
from outside access by hiding it inside a class.
We make variables private and allow access using public getter and setter methods.
Real-life example: A medicine capsule
hides the drug inside — only what's needed is exposed.
Java Program
Example – Encapsulation:
class Student {
private String
name; // private = hidden from outside
// Setter method
public void
setName(String newName) {
name = newName;
}
// Getter method
public String
getName() {
return name;
}
}
public class Main {
public static
void main(String[] args) {
Student s = new
Student();
s.setName("John"); //
Set name
System.out.println(s.getName());
// Get name → Output: John
}
}
Explanation: The name variable is hidden, and we can only access it through the getName() and setName() methods.
Polymorphism
Polymorphism means "many forms". It
allows the same method name or operator to behave differently depending on the
context.
It is of two types:
- Compile-time Polymorphism → Method Overloading
- Run-time Polymorphism → Method Overriding
Real-life
example: A person can
behave like a student in class, a friend outside, and a customer in a shop —
same person, different roles.
Java Program
Example – Method Overloading (Compile-time):
class Math {
int add(int a,
int b) {
return a + b;
}
double add(double
a, double b) {
return a + b;
}
}
public class Main {
public static
void main(String[] args) {
Math m = new
Math();
System.out.println(m.add(5, 6));
// Output: 11
System.out.println(m.add(2.5, 3.5));
// Output: 6.0
}
}
Java Program Example – Method Overriding
(Run-time):
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static
void main(String[] args) {
Animal a = new
Cat();
a.sound(); // Output: Cat meows
}
}
Explanation:
The same method name sound() behaves differently for Animal and Cat.
Comments
Post a Comment