Thursday, June 18, 2026

2. 0 java Class

}  Class can be defined in multiple ways

Ê  A class is a blueprint for an object.

Ê  A class is a user-defined data type.

Ê  A class is a collection of objects of the similar kind.

}  A class is a user-defined data type that contains:

Ê  Attributes (Variables) → Data(data members

Ê  Methods (Functions) → Behavior(member functions



}  Class name can be given as per the Identifier Naming Conventions.

Defining a Class in Java

All it takes to define a class in Java is this:

public class MyClass {

 

}

This defines a public Java class called MyClass. The class has no fields, constructors or methods.

.java Files

The above class definition should be put in its own file named MyClass.java. Java files should be named the same as the name of the class they contain, with the .java as file name extension. Make sure you keep the same uppercase and lowercase characters from the class name in the file name too.

Only put a single class definition in each Java file, unless your class contains inner classes of some kind.

Class With Fields

As mentioned earlier, a Java class can contain data in the shape of variables. Variables that belong to the class are typically called "fields".

The next example shows a Java class which is to model a car. Therefore the class has named Car and has three fields. Here is the Java class in code:

public class Car {

 

    public String brand = null;

    public String model = null;

    public String color = null;

 

}

This code defines a Java class named Car. The Car class has three fields. The Car class has no methods. Only field declarations. Fields are described in more detail in the text on java fields

Class With Constructor

A Java class can have a constructor. A constructor is a special method that is called when an object of the given class is created (explained later). The purpose of a constructor is to initialize the fields in the class. The fields are also called the "internal state". Here is an example of a Java class with two constructors:

public class Car {

     public String brand = null;

    public String model = null;

    public String color = null;

     public Car() {

    }

public Car(String theBrand, String theModel, String theColor) {

        this.brand = theBrand;

        this.model = theModel;

        this.color = theColor;

    }

 

}

The constructors are the two methods that have the same name as the class, and which have no return type specified. The first constructor takes no parameters, and the second takes 3 parameters. The constructor that takes 3 parameters stores the values of these parameters in the fields of the created object. Constructors are covered in more detail in constructor tutorial

Class With Methods

A Java class can also contain operations. These operations are typically called methods. A Java method contains Java instructions that typically perform some operations on a field in the class, or on one of the parameters (also variables) values passed to the method when the method was called.

Here is the Java class, Car example from the previous section with a method added:

public class Car {

 

    public String brand = null;

    public String model = null;

    public String color = null;

 

    public void setColor(String newColor) {

        this.color = newColor;

    }

}

In the class definition above I have added a setColor() method. When called, this method sets the internal color variable (field) to a new value. Methods are described in more detail in the text on methods

Class With Nested Class

As mentioned earlier, you can define a nested class inside another Java class.

Here is an example of defining a nested class inside a Java class:

public class MyClass {

 

    public static class MyNestedClass{

 

    }

}

In the example above, the outer class is called MyClass and the nested class is called MyNestedClass .

Neither of the classes in this example has any fields or methods, but both the outer and nested class could have as many fields and methods as you see fit.

 


Phases of a Java Program

 

  Phase 1: Creating/editing a program

        The editing phase is the stage where the programmer writes the Java source code with editor.

        The following are some Java Integrated Development Environments (IDEs).

       Jbuilder, NetBeans , Sun ONE Studio, Eclipse, jEdit, Jcreator, BlueJ, jGRASP, etc.

       Most of this editors  can be downloaded for free.

       Any text editor can also be used for creating Java programs.

       The filename must match the public class name.

       Programs written on IDEs  or any other editors are known as Source Code.

       Java source code file names end with .java extension

Phase 2: Compiling Java Programs into Bytecodes

        Since a computer cannot understand a source program, program called a compiler is used to translate the source program into a machine language program called an object program.

        With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode.

o   The Java compiler (javac) checks the code.

o   It detects syntax errors.

o   It converts .java file into .class file.

        To compile Java Program

o   Javac welcome.java

        To execute java programs

o   Java welcome

  • This .class file contains bytecode, not machine code.
  • If there are syntax errors, compilation fails.

 Phase 3: Loading a program

        A program must be placed in memory before it can execute : a process known as loading.

        The class loader takes/locates  the .class file (produced during compilation process) containing  the program’s bytecodes and  transform them to primary memory.

        The class loader also loads any of the .class files provided by java that your program uses.

        It prepares them for execution.;

        The .class files can be loaded from a disk on your system or over a network.

Phase 4 : Bytecode Verification

        Involves examining bytecodes to ensure that they are valid and do not violate Java’s security restriction.

        Java enforces strong security, to make sure that Java programs arriving over the network do not damage your files or your system (as computer viruses and worms might).

Phase 5: Execution: the program actually runs.

        The JVM executes the program’s bytecodes, thus performing the actions specified by the program.

        .class file is not final machine code yet. It is virtual machine code. JVM need a second compilation to convert virtual machine code to real machine code. We call it just-in-time compiler (JIT).

        This greatly affects the speed of the execution.

        Traditionally, our C/C++ program can finish all the compilation before execution. The generated machine code could run directly on OS or hardware.

 


1.5 JVM, JDK and JRE

                                          JDK (Java Development Kit)

        The Java Development Kit (JDK) is a software development package used to develop, compile, and run Java applications.

        JDK Includes a complete JRE (Java Runtime Environment) plus tools for developing, debugging, and monitoring Java applications.

        JDK is required to build and run Java applications and applets.

  

The diagram shows how Java (JDK) relates to the JVM and JRE:

  • JDK = tools + runtime for developing Java programs
    • It includes JRE (the runtime to run Java) plus development tools like javac, jar, etc.
  • JRE = Java runtime environment to run Java apps
    • It contains the JVM (executes bytecode) and the libraries/other files needed to run them.

         Components of JDK

1. Java Compiler (javac)

Converts source code (.java) into bytecode (.class)

2. Java Runtime Environment (JRE)

Provides environment to run Java programs

3. JVM (Java Virtual Machine)

Executes bytecode

4. Development Tools


Java Runtime Environment (JRE)

        The Java Runtime Environment (JRE) is the software package required to run Java programs.

        JRE : provides the libraries((API)), the Java Virtual Machine, and other components to run applets and applications written in the Java programming language.

        JRE does not contain tools and utilities such as compilers or debuggers for developing applets and applications.

        jre

 

Java virtual Machine(JVM)

        Java virtual Machine(JVM) is a virtual Machine that provides runtime environment to execute java byte code. 

        It converts bytecode (.class files) into machine code..

        JVMs are available for many hardware and software platforms. The JVM performs following main tasks:

         Loads class files: The JVM loads compiled .class files into memory so they can be prepared for execution.

        Verifies bytecode: The JVM checks the bytecode to ensure it is valid, safe, and follows Java’s security and language rules before execution.

        Executes bytecode: The JVM translates bytecode into machine-level instructions and runs the program on the host system.

        Manages memory: The JVM allocates, organizes, and automatically reclaims memory using garbage collection.

        Provides security: The JVM protects the system by preventing unauthorized access, illegal operations, and unsafe code execution.

 

 






Monday, June 15, 2026

1.4. Editing, Compiling and Interpreting

 Editing, Compiling and Interpreting

1. Editing

Editing is the process of writing or modifying source code using a text editor or an Integrated Development Environment (IDE).

Common Editors and IDEs

  • Visual Studio Code
  • Eclipse
  • NetBeans
  • IntelliJ IDEA

Real-World Example

Editing a program is similar to writing or revising a document in Microsoft Word before printing it.

2. Compiling

Compiling is the process of translating the entire source code into machine code before execution.

How It Works

Source Code
     
   Compiler
     
 Machine Code
     
   Execution

Real-World Example

Compiling is like translating an entire English book into Amharic before anyone starts reading it.

Advantages

  • Faster execution.
  • Better performance.
  • Errors detected before running.

Example Compiled Languages

  • C ,  C++ ,  Java

3. Interpreting

Interpreting is the process where source code is translated and executed line by line during program execution.

How It Works

Source Code
     
 Interpreter
     
 Execute Line by Line

Real-World Example

An interpreter is like a live translator who translates each sentence immediately while a speaker is talking.

Advantages

  • Easier debugging.
  • Platform independence.
  • Faster testing and development.

Example Interpreted Languages

  • Python , JavaScript  , Ruby

 

1.3. Overview of object oriented principles

 Object-Oriented Programming is built on the following fundamental principles.


object :

        Any entity that has state and behavior is known as an object.

        An object has three characteristics:

        state(data/attributes): represents data (value) of an object.

        behavior(methods/functions): represents the behavior (functionality) of an object such as deposit, withdraw etc.

        identity: Object identity is typically implemented via a unique ID.

        Example, a chair, pen, table, keyboard, bike, etc.

        It can be physical or logical.

        Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.

 

 class

  •    A class is a group of objects that has common properties.
  •  class is a template or blueprint from which objects are created.

Inheritance

  • Inheritance allows one class to acquire the properties and behaviors of another class.
  •     Inheritance is a mechanism in which one object acquires all the properties and behaviours of parent object.
  •    It provides code reusability.

 Encapsulation

        Encapsulation is a mechanism where you bind your data and code together as a single unit (class) and restricting direct access to data..

        Why Encapsulation?

o   Protects data

o   Improves security

o   Controls how data is accessed

         Data encapsulation is basically used for achieving data/information hiding i.e., security.

Real-World Example

  • 1. An ATM machine hides its internal processes. Users only interact through buttons and screens without knowing how the internal system works.
  • 2. :  a medicine capsule having different components packed as a single unit. In Java

                      Abstraction

        Abstraction refers to hiding the details and showing the essential things(features) to the user.

        If you look at the image below, whenever we get a call, we get an option to either pick it up or just reject it. But in reality, there is a lot of code that runs in the background.

        So you don’t know the internal processing of how a call is generated, that’s the beauty of abstraction.

example 1: phone call

2. When driving a car, you use the steering wheel, brake, and accelerator without knowing the internal engine mechanisms.

Polymorphism

Polymorphism allows the same method or interface to perform different actions depending on the object using it.

        Polymorphism means taking many forms, where ‘poly’ means many and ‘morph’ means forms.

         It is the ability of a variable, function or object to take on multiple forms. In other words,

        Polymorphism means more than one forms.

 Real-World Example

        A person can perform different roles:

        Teacher in a classroom.

        Parent at home.

        Customer in a store.

        The same person behaves differently depending on the situation.

Benefit

        Flexibility.

        Extensibility.

        Easier program design.


 

 




2. 0 java Class

}   Class can be defined in multiple ways Ê   A class is a blueprint for an object. Ê   A class is a user-defined data type. Ê   A cla...