0% found this document useful (0 votes)
37 views

Java Basic Notes

The document discusses procedural programming (POP) and object-oriented programming (OOP). POP focuses on procedures and functions that operate on data, while OOP emphasizes objects that encapsulate both data and behavior. The key differences are that OOP promotes concepts like encapsulation, inheritance, and polymorphism. It also organizes code based on objects and their interactions rather than step-by-step execution. The document then provides details about Java, including its platform independence, object-oriented features, syntax, run cycle involving compilation and execution, types of constructors, and access modifiers.

Uploaded by

aliroomanq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Java Basic Notes

The document discusses procedural programming (POP) and object-oriented programming (OOP). POP focuses on procedures and functions that operate on data, while OOP emphasizes objects that encapsulate both data and behavior. The key differences are that OOP promotes concepts like encapsulation, inheritance, and polymorphism. It also organizes code based on objects and their interactions rather than step-by-step execution. The document then provides details about Java, including its platform independence, object-oriented features, syntax, run cycle involving compilation and execution, types of constructors, and access modifiers.

Uploaded by

aliroomanq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

23TL010

OOP Notes
What is Procedural oriented programming?
Pop (procedural oriented programming) is the oldest form of programming where a
program is structured around procedures or routines that operate on data. In this
paradigm, the focus is on breaking down the program into smaller, manageable
procedures that perform specific tasks. It's characterized using procedures, functions,
or subroutines to organize and manipulate data, with an emphasis on step-by-step
execution of instructions. Popular examples of procedural-oriented languages include
C, BASIC, FORTRAN, and Pascal etc.
What is OOP?
Object-oriented programming (OOP) is a programming paradigm based on the concept
of "objects," which can contain data, in the form of fields or attributes, and code, in the
form of procedures or methods. Objects are instances of classes, which act as
blueprints for creating objects. OOP emphasizes concepts such as encapsulation,
inheritance, polymorphism, and abstraction, which help in organizing and managing
complex software systems more effectively. It provides a way to structure code in a
modular and reusable manner, making it easier to maintain and scale applications.
Some popular object-oriented programming languages include Java, C++, Python, and
C#.
DIFFERENCE BETWEEN OOP AND POP.
The following are major differences between OOP and POP.

Procedural-oriented programming Object-oriented programming


Focuses on procedures or functions that Focuses on objects that encapsulate
operate on data. data and behavior.
Data and functions are separate. Promotes data abstraction and
encapsulation.
Does not emphasize concepts like Supports features like inheritance and
inheritance and polymorphism. polymorphism.
Code organization is based on step-by- Code organization is based on objects
step execution. and their interactions.
In essence, while POP focuses on procedures and functions, OOP emphasizes objects
and their interactions, leading to code that is often more modular, reusable, and easier
to maintain in larger projects.
23TL010
Features of Java and basic syntax with run cycle.
Java is a versatile and widely used programming language known for its platform
independence, object-oriented paradigm, robustness, and simplicity. Here are some
key features of Java:

1. Platform Independence: Java programs can run on any device or operating


system that has a Java Virtual Machine (JVM) installed. This is achieved through
the compilation of Java source code into bytecode, which is then executed by the
JVM.
2. Object-Oriented Programming (OOP): Java is primarily an object-oriented
language, which means it emphasizes the use of objects to represent data and
functionality. Key OOP concepts in Java include classes, objects, inheritance,
polymorphism, and encapsulation.
3. Simple and Familiar Syntax: Java syntax is derived from C and C++, making it
familiar to programmers with experience in those languages. Java intentionally
omits complex features like pointers and operator overloading to enhance
simplicity and readability.
4. Robustness and Reliability: Java was designed with a focus on reliability and
robustness. It includes features like automatic memory management (garbage
collection), exception handling, and strong type checking to prevent common
programming errors and enhance program stability.
5. Security: Java's design incorporates security features at various levels, including
bytecode verification by the JVM to prevent unauthorized code execution, a built-
in security manager to control access to system resources, and a class loader
architecture for loading classes from trusted sources.
6. Platform Libraries (APIs): Java provides a rich set of standard libraries
(Application Programming Interfaces or APIs) for common tasks such as
input/output operations, networking, database access, and graphical user
interface (GUI) development. These libraries simplify programming tasks and
promote code reuse.
7. Portability and Scalability: Java's platform independence and robustness make
it suitable for developing applications that need to run on various devices and
scale to accommodate changing requirements.
8. Backward Compatibility: Java maintains backward compatibility, ensuring that
newer versions of the language can run code written in older versions with
minimal or no modifications. This helps to protect investments in existing Java
codebases and promotes the adoption of new language features.

These features underpin the design and usage of Java, making it a popular choice for a
wide range of applications, from enterprise software development to mobile and web
development.
23TL010
Run cycle and basic syntax.
In Java, the execution of a program generally follows a predefined cycle, which involves
several stages that are defined below.
1. Write: Developers create Java source code using a text editor or an Integrated
Development Environment (IDE).
2. Compile: The Java source code is compiled into bytecode using the Java
compiler (javac), resulting in .class files.
3. Load and Execute: The Java Virtual Machine (JVM) loads the bytecode, verifies it,
and executes it. This involves loading classes, linking them, and initializing them
as necessary.
4. Run: The main method of the specified class is executed, serving as the entry
point of the program.
5. Terminate: The program terminates either when the main method completes its
execution or when an explicit termination command like System.exit() is
encountered.

This cycle repeats each time a Java program is run, with the JVM managing the
execution process.

BASIC SYNTAX.
A basic Java program typically includes a class with a main method that prints "Hello, World!" to
the console. Here's the simplest Java program:

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

}

public class HelloWorld: Declares a class named HelloWorld. By convention, the filename
should match the class name.
• public static void main(String[] args): Declares the main method, which serves as the
entry point of the program. It must be public, static, and void, and it accepts a String array as
arguments.
• System.out.println("Hello, World!");: Outputs "Hello, World!" to the console. System.out
refers to the standard output stream, and println() is a method to print a line.
23TL010
Constructor and its types.
In Java, a constructor is a special type of method that is automatically called when an
instance (object) of a class is created. Its main purpose is to initialize the newly created
object. Constructors have the same name as the class and do not have a return type,
not even void.

There can be four types of constructors in Java.


1. non-parameterized constructor: A constructor which has no argument is
known as non-parameterized constructor (or no-argument constructor). It is
invoked at the time of creating an object. If we don’t create one, then it is created by
default by Java.
class Student {

String name;

int age;

Student() {

System.out.println("Constructor called");

}
}

2. Parameterized constructor: Constructor which has parameters is called a


parameterized constructor. It is used to provide different values to distinct objects.
class Student {

String name;

int age;

Student(String name, int age) {

this.name = name;

this.age = age;

}
23TL010
3. Copy Constructor: This type of constructor creates a new object by copying the
state of an existing object. It takes an object of the same class as a parameter.

public class MyClass {

private int myVariable;

// Copy constructor
public MyClass(MyClass original) {
this.myVariable = original.myVariable;
}
}

4. Default Constructor: If a class does not explicitly define any constructors, Java
provides a default constructor. This constructor initializes instance variables to
their default values (0 for numeric types, null for reference types, false for
Boolean).

public class MyClass {


// Default constructor
public MyClass() {
// Initialization code here
}
}

These are the main types of constructors in Java, each serving different purposes
based on the requirements of the class.
23TL010
What are access modifiers.

In Java, access modifiers are keywords that specify the accessibility of classes,
variables, methods, and constructors. There are four types of access modifiers.

Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.

Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.

Protected: The access level of a protected modifier is within the package and outside
the package through child class. If you do not make the child class, it cannot be
accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
class Account {
public String name;
protected String email;
private String password;

public void setPassword(String password) {


this.password = password;
}

// Getter method for password (for demonstration purposes)


public String getPassword() {
return this.password;
}
}

public class Main {


public static void main(String args[]) {
Account a1 = new Account();
a1.name = "Ali";
a1.setPassword("abcd");
a1.email = "[email protected]";

// Displaying the details


System.out.println("Name: " + a1.name);
System.out.println("Email: " + a1.email);
// Since password is private, we use getPassword() method to retrieve
it.
System.out.println("Password: " + a1.getPassword());
}
}
}
23TL010
Basic principles of Java.
As java is an object-oriented programming language so it follows the key principles of
OOPs which includes

Encapsulation: Encapsulation is the process of wrapping data into single unit or the
process of binding data and function together into single unit called class. In
encapsulation data is not accessible directly in other words data is kept private and
public getters and setters are used to manipulate information among them
public class Person {
// Private variables (data)
private String name;
private int age;

// Public constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}

// Public getter method for name


public String getName() {
return name;
}

// Public setter method for name


public void setName(String name) {
this.name = name;
}

// Public getter method for age


public int getAge() {
return age;
}

// Public setter method for age


public void setAge(int age) {
this.age = age;
}
}

You might also like