Oop Java
Oop Java
Oop Java
Objects:
Definition: Objects in Java are instances of classes, representing real-world entities.
They encapsulate data and behavior, allowing for modularity and reusability in code. Objects enable the
principles of OOP: encapsulation, inheritance, polymorphism, and abstraction. They enhance code
organization, readability, and maintainability, making complex systems manageable and adaptable.
1. Data field
2. Methods/Function
3. Construction //it can contain other things
4.
Example: className object = new className(); //any task perform will come in class
1) Class person {
String name;
int age;
Comments:
Textual annotations in Java providing explanations within code, ignored by the compiler.
// Creating Attributes
int x = 10;
int age = 29;
String name = "Ali";
m.display_message();
m.show_x_value();
m.display_number(5);
m.display_text("Ali");
Package Declarations: Declaration specifying the package to which the class belongs.
Import Statements: Statements used to import classes from other packages.
Interface Definitions: Definitions of interfaces (can be outside classes).
Enum Declarations: Enumerated types specifying a set of constants (can be outside classes).
Helper Functions/Utility Methods: Functions or methods not tied to a specific class, often defined in
separate utility classes or files.
Other Class Definitions: Definitions of separate classes that are not part of the current class hierarchy.
Example:
public class Main {
Constructor definition:
Constructor in Java is a special type of method used to initialize objects. It is invoked automatically when an object is
created.
1) Default Constructor: It is called when we create an object without specifying any arguments. This constructor is
automatically created by Java if no other constructor is defined explicitly.
2) No-Argument Constructor: Similar to the default constructor, it initializes the object when no arguments are
provided. This constructor might perform certain default actions.
3) Parameterized Constructor: A constructor that takes arguments during object creation is called a parameterized
constructor. It allows the initialization of an object with specific values passed as arguments.
Example:
class Employee {
int emp_id;
// Non-argument Constructor
Employee() {
// Body can be empty or you can put any logic here
System.out.println("Employee without id created");
}
// Argument Constructor
Employee(int id) {
emp_id = id;
System.out.println("My Employee id is " + emp_id);
}
// Argument Constructor
Employee(int id, String name) {
System.out.println("My Employee id: " + id);
System.out.println("My Employee name is " + name);
}
}
Example:
class Main {
public static void main(String[] args) {
Type Casting
Type casting refers to the process of converting a value from one data type to another in Java.
1. Widening Casting (Automatically): Widening Casting (Automatically) refers to the implicit conversion of a
smaller data type to a larger data type size without the need for explicit type conversion in Java.
Example:
int smallNumber = 10;
long largerNumber = smallNumber; // Widening casting (int to long)
2. Narrowing Casting (Manually): Narrowing Casting (Manually) is the explicit conversion of a larger data
type into a smaller data type in Java, potentially leading to data loss or truncation.
For example:
double largeDecimal = 123.456;
int smallNumber = (int) largeDecimal; // Narrowing casting (double to int)
3. Widening Casting: Implicit conversion of a smaller data type into a larger data type size without data loss.
Example:
int smallNumber = 10;
long largerNumber = smallNumber; // Widening casting (int to long)
Static and Non-static (variable and methods)
Static:
Static is used for memory management and it men with static keyword. It belong to the class instead of an
instance of class (object).
Only one instance will be created throughout the project.
We need when we don’t want to change it frequently and it can be used in all over the applications.
Example:
public class User {
// Static Variables
public static String bankName = "HBL"; // Corrected variable name to follow conventions
public static int userAccount; // Corrected variable name to follow conventions
// Non-Static Variables
public int userBalance = 0; // Corrected variable name to follow conventions
public String userName;
// Calling static method and setting static variable using class name
User.userAccount = 2; // Set user account using class name
Example Code Snippets: Demonstrative snippets showcasing class and object creation, error types,
comments,primitive data types, methods, and constructors in Java.
// Parent class
class Humanbeing {
public String name;
protected int age;
// Child class
class Man extends Humanbeing {
public String gender;
// Main class
class Main {
public static void main(String[] args) {
Man manObj = new Man("John", 30, "Male"); // Creating a Man object
manObj.age = 19;
manObj.gender = "Male";
manobj.display();
}
}
Inheritance
Inheritance is one of the four pillars of object-oriented programming because it allows the creation of
hierarchical classifications.
Using inheritance, a general class can be created that defines traits common to a set of related items. This class
can then be inherited by other, more specific classes, each adding those things that are unique to it.
In the terminology of Java;
o A class that is inherited is called a superclass.
o The class that does the inheriting is called a subclass.
Therefore, a subclass is a specialized version of a superclass. It inherits all of the variables and methods
defined by the superclass and adds its own, unique elements.
Type of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance*(Not supported in Java)
5. Hybrid/Combination Inheritance
Encapsulation:
The process of binding data and corresponding methods (behavior) together into a single unit is Called
encapsulation.
To achieve encapsulation:
1. Declare class variables/attributes as private: This restricts direct access to these variables from outside the
class.
2. Provide public getter and setter methods:
Getter methods: These methods retrieve or provide access to the private variables.
Setter methods: These methods modify or update the value of the private variables.
Example:
class Customer {
private long accNo;
private String name;
private float balanceAmount;
cust.setName("Abdul Haseeb");
cust.setAccNo(12345678);
cust.setBalanceAmount(250000.6f);
UMC consist of
1. Class name
2. Attribute
3. Method name
UMC Diagram:
Customer
NCC NO: Long
Name: String
balAmount: float
set Accno(long)
get Accnp( )
set name(string)
get name( )