-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abff578
commit b88b654
Showing
4 changed files
with
622 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
docs/programming-fundamentals/language-syntax/classes.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
--- | ||
id: classes | ||
sidebar_position: 8 | ||
title: Classes | ||
sidebar_label: Classes | ||
description: "Learn about classes and object-oriented programming concepts in JavaScript, Java, Python, and C++. Understand how to create, instantiate, and work with classes effectively in these popular languages." | ||
tags: [classes, object-oriented programming, programming, syntax, js, java, python, cpp] | ||
--- | ||
|
||
Classes are fundamental to object-oriented programming (OOP). They provide a blueprint for creating objects that encapsulate data and behavior. This guide covers the basics and key aspects of classes in JavaScript, Java, Python, and C++ with practical examples. | ||
|
||
<Ads /> | ||
|
||
## What is a Class? | ||
|
||
A class is a template for creating objects (instances) that share common properties and methods. It defines the structure and behavior of the objects. Classes promote code reusability and help in organizing code in a modular way. | ||
|
||
## Classes in Different Languages | ||
|
||
<Tabs> | ||
<TabItem value="javascript" label="JavaScript" default> | ||
|
||
### JavaScript Classes Overview | ||
|
||
JavaScript introduced class syntax in ES6. While it is syntactic sugar over JavaScript’s existing prototype-based inheritance, it provides a clear and readable way to create objects. | ||
|
||
#### Class Declaration | ||
|
||
```js title="JavaScript Class Example" | ||
// Declaration of a class | ||
class Person { | ||
constructor(name, age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
// Method | ||
greet() { | ||
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`); | ||
} | ||
} | ||
|
||
// Instantiation | ||
const person1 = new Person('Alice', 30); | ||
person1.greet(); // Output: Hello, my name is Alice and I am 30 years old. | ||
``` | ||
|
||
#### Inheritance | ||
|
||
```js title="JavaScript Inheritance Example" | ||
class Employee extends Person { | ||
constructor(name, age, jobTitle) { | ||
super(name, age); // Call the parent class constructor | ||
this.jobTitle = jobTitle; | ||
} | ||
|
||
describeJob() { | ||
console.log(`I am a ${this.jobTitle}.`); | ||
} | ||
} | ||
|
||
const employee1 = new Employee('Bob', 25, 'Software Developer'); | ||
employee1.greet(); // Output: Hello, my name is Bob and I am 25 years old. | ||
employee1.describeJob(); // Output: I am a Software Developer. | ||
``` | ||
|
||
</TabItem> | ||
|
||
<TabItem value="java" label="Java"> | ||
|
||
### Java Classes Overview | ||
|
||
In Java, classes are the building blocks of OOP. They encapsulate data (fields) and behavior (methods). | ||
|
||
#### Class Declaration | ||
|
||
```java title="Java Class Example" | ||
public class Person { | ||
private String name; | ||
private int age; | ||
|
||
// Constructor | ||
public Person(String name, int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
// Method | ||
public void greet() { | ||
System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); | ||
} | ||
} | ||
|
||
// Instantiation | ||
public class Main { | ||
public static void main(String[] args) { | ||
Person person1 = new Person("Alice", 30); | ||
person1.greet(); // Output: Hello, my name is Alice and I am 30 years old. | ||
} | ||
} | ||
``` | ||
|
||
#### Inheritance | ||
|
||
```java title="Java Inheritance Example" | ||
public class Employee extends Person { | ||
private String jobTitle; | ||
|
||
public Employee(String name, int age, String jobTitle) { | ||
super(name, age); // Call the parent class constructor | ||
this.jobTitle = jobTitle; | ||
} | ||
|
||
public void describeJob() { | ||
System.out.println("I am a " + jobTitle + "."); | ||
} | ||
} | ||
|
||
// Usage | ||
public class Main { | ||
public static void main(String[] args) { | ||
Employee employee1 = new Employee("Bob", 25, "Software Developer"); | ||
employee1.greet(); // Output: Hello, my name is Bob and I am 25 years old. | ||
employee1.describeJob(); // Output: I am a Software Developer. | ||
} | ||
} | ||
``` | ||
|
||
</TabItem> | ||
|
||
<TabItem value="python" label="Python"> | ||
|
||
### Python Classes Overview | ||
|
||
In Python, classes are easy to define and work with, thanks to its simple and intuitive syntax. | ||
|
||
#### Class Declaration | ||
|
||
```python title="Python Class Example" | ||
class Person: | ||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age | ||
|
||
def greet(self): | ||
print(f"Hello, my name is {self.name} and I am {self.age} years old.") | ||
|
||
# Instantiation | ||
person1 = Person("Alice", 30) | ||
person1.greet() # Output: Hello, my name is Alice and I am 30 years old. | ||
``` | ||
|
||
#### Inheritance | ||
|
||
```python title="Python Inheritance Example" | ||
class Employee(Person): | ||
def __init__(self, name, age, job_title): | ||
super().__init__(name, age) # Call the parent class constructor | ||
self.job_title = job_title | ||
|
||
def describe_job(self): | ||
print(f"I am a {self.job_title}.") | ||
|
||
# Usage | ||
employee1 = Employee("Bob", 25, "Software Developer") | ||
employee1.greet() # Output: Hello, my name is Bob and I am 25 years old. | ||
employee1.describe_job() # Output: I am a Software Developer. | ||
``` | ||
|
||
</TabItem> | ||
|
||
<TabItem value="cpp" label="C++"> | ||
|
||
### C++ Classes Overview | ||
|
||
C++ provides robust support for OOP, allowing you to create complex programs using classes. | ||
|
||
#### Class Declaration | ||
|
||
```cpp title="C++ Class Example" | ||
#include <iostream> | ||
#include <string> | ||
|
||
class Person { | ||
private: | ||
std::string name; | ||
int age; | ||
|
||
public: | ||
// Constructor | ||
Person(std::string name, int age) : name(name), age(age) {} | ||
|
||
// Method | ||
void greet() const { | ||
std::cout << "Hello, my name is " << name << " and I am " << age << " years old." << std::endl; | ||
} | ||
}; | ||
|
||
// Instantiation | ||
int main() { | ||
Person person1("Alice", 30); | ||
person1.greet(); // Output: Hello, my name is Alice and I am 30 years old. | ||
return 0; | ||
} | ||
``` | ||
|
||
#### Inheritance | ||
|
||
```cpp title="C++ Inheritance Example" | ||
class Employee : public Person { | ||
private: | ||
std::string jobTitle; | ||
|
||
public: | ||
Employee(std::string name, int age, std::string jobTitle) : Person(name, age), jobTitle(jobTitle) {} | ||
|
||
void describeJob() const { | ||
std::cout << "I am a " << jobTitle << "." << std::endl; | ||
} | ||
}; | ||
|
||
// Usage | ||
int main() { | ||
Employee employee1("Bob", 25, "Software Developer"); | ||
employee1.greet(); // Output: Hello, my name is Bob and I am 25 years old. | ||
employee1.describeJob(); // Output: I am a Software Developer. | ||
return 0; | ||
} | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
<AdsComponent /> | ||
|
||
## Best Practices | ||
|
||
1. **Encapsulation**: Keep class attributes private and use methods to modify them safely. | ||
2. **Inheritance**: Use inheritance to promote code reusability and hierarchy. | ||
3. **Polymorphism**: Implement polymorphism to enhance flexibility and maintainability. | ||
4. **Constructor Overloading**: In languages that support it, provide multiple constructors for different initialization needs. | ||
|
||
|
||
## Conclusion | ||
|
||
Classes are a core concept in OOP, enabling you to model real-world entities effectively. Understanding how to create, instantiate, and work with classes is essential for building robust and maintainable software applications. | ||
|
||
<AdsComponent /> | ||
|
||
--- | ||
|
||
<h2 className="text-center">Feedback and Support</h2> | ||
|
||
<GiscusComponent /> |
Oops, something went wrong.