Skip to main content

 

 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");

  }

}

 class Cat extends Animal {

  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

Popular posts from this blog

Function of OS 1. Process Management The operating system helps in running many programs at the same time. It keeps track of each running program (called a process), decides which one should run next, and stops or starts them as needed. It makes sure that all the programs get a fair chance to use the CPU.   2. Memory Management The OS manages the computer's memory (RAM). It decides which program will use how much memory and keeps track of it. When a program is closed, it frees up the memory so that other programs can use it. This helps the computer run smoothly without crashing.   3. File System Management The operating system helps us to create, save, open, and delete files. It organizes files in folders and keeps them safe. It also controls who can open or edit a file to protect our data.   4. Device Management The OS controls all the input and output devices like the keyboard, mouse, printer, and monitor. It tells the devices what to do and makes su...

UNIT 3 Array in C#

  Array in C# An array is a collection of variables of the same data type , stored in contiguous memory locations , and accessed using a common name and index . Each item in an array is called an element , and the index of arrays in C# starts from 0 .     Key Points About Arrays in C#: 1.       Elements are stored in contiguous memory locations. 2.       Index starts from 0. So, for an array of size 5, valid indexes are 0 to 4. 3.       Arrays are reference types and are allocated on the heap. 4.       C# array is an object of base type System.Array . 5.       Array elements can be of any type , including another array (array of arrays). 6.       Jagged array (array of arrays) elements are reference types and are initialized to null . 7.       Arrays can be single-dimensi...

Unit 2 Java

Data Types A data type defines the kind of data a variable can store, the operations that can be performed on it, and the amount of memory allocated. Java Primitive Data Types Data Type Size Range (Approx.) Example Byte 1 byte -128 to 127 byte a = 100; short 2 bytes -32,768 to 32,767 short s = 1000; Int 4 bytes -2,147,483,648 to 2,147,483,647 int num = 50000; Long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 long l = 100000L; float 4 bytes ~6–7 decimal digits float f = 5.75f; double 8 bytes ~15 decimal digits double d = 19.99; Char 2 bytes Single Unicode character char c = 'A'; boolean 1 bit* tru...