Java Programming: Classes, Objects, and Key Concepts
Classified in Computers
Written at on English with a size of 5.28 KB.
Classes (الصفوف)
- A class consists of variables (fields) and methods.
- Variables are data members of a class.
- Methods are functions that define the class's behavior.
Variables (المتغيرات)
- Declared with a data type and a name.
- Can be public or private.
- Examples:
int age
,String name
.
Methods (الأساليب)
- Functions that perform specific tasks.
- Can have parameters and return values.
- Types:
- Void methods: Don't return a value.
- Return type methods: Return a value.
- Static methods: Can be called without creating an object.
- Instance methods: Require an object to be called.
- Abstract methods: Declared without a body; used in abstract classes.
- Overloaded methods: Multiple methods with the same name but different parameters.
Constructors (البناؤون)
- Special methods used to initialize objects.
- No return type.
- Can be default (no parameters) or parameterized.
Method Overloading (زيادة التحميل)
- Having multiple methods with the same name but different parameters.
The compiler determines which method to call based on the arguments passed.
Arrays (المصفوفات)
- Collections of elements of the same data type.
- Can be one-dimensional or multidimensional.
- Access elements using indices.
Example Class
Java
class Students {
private String name;
private int grade;
public Students() { // Default constructor
// Initialize variables
}
public Students(String name, int grade) { // Parameterized constructor
this.name = name;
this.grade = grade;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setGrade(int grade) {
this.grade = grade;
}
public int getGrade() {
return grade;
}
public String toString() {
return "Name: " + name + ", Grade: " + grade;
}
}
Abstraction (التجريد)
- Hiding internal details and providing a simple interface for users.
- Achieved using abstract classes and interfaces.
- Example:
Java
abstract class Animal {
abstract void sound();
}
class Cat extends Animal {
void sound() {
System.out.println("Meow");
}
}
Inheritance (الوراثة)
- A class inherits properties and methods from another class.
- Keyword
extends
is used for inheritance. - Example:
Java
class A {
int a;
}
class B extends A {
int b;
}
Interfaces (الواجهات)
- A template that defines a set of methods that a class must implement.
- Supports multiple inheritance.
- Example:
Java
interface Animal {
void sound();
}
class Dog implements Animal {
void sound() {
System.out.println("Bark");
}
}
Enums (الثوابت)
- A data type that represents a set of constants.
- Each object can have only one value from the enum.
- Example:
Java
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Class Vector
- A dynamic array that can grow or shrink in size.
- Stores objects of different types.
- Example:
Java
import java.util.Vector;
Vector<Integer> v = new Vector<>(15); // Capacity of 15 elements
Important Methods in Vector:
add(E e)
: Adds an element to the vector.remove(int index)
: Removes an element at a specific index.size()
: Returns the number of elements in the vector.get(int index)
: Returns the element at a specific index.indexOf(Object o)
: Returns the index of the first occurrence of an element.contains(Object o)
: Checks if the vector contains an element.isEmpty()
: Checks if the vector is empty.clear()
: Removes all elements from the vector.
Class Array
- A static array with a fixed size.
- Stores elements of the same type.
- Example:
Java
int[] array = new int[10]; // Array of 10 integers
Important Methods in Arrays:
Arrays.binarySearch(int[] array, int key)
: Searches for a value in a sorted array.Arrays.equals(int[] a, int[] b)
: Compares two arrays for equality.Arrays.fill(int[] a, int val)
: Fills an array with a value.Arrays.sort(int[] a)
: Sorts an array in ascending order.
Additional Notes
- Use meaningful variable and method names.
- Indent code properly for readability.
- Add comments to explain code logic.
Remember: This is a basic overview. Java programming involves many more concepts and features.