So in programming, Class types refers to the classification of classes based on their purpose, behavior, or implementation style.
Classes are blueprints for creating objects and can be organized into different types based on their use in Object-Oriented programming (OOP)
Here's the main types of classes :
1.Concrete Class :
This is the most common type of classes used to create objects directly. Not all methods and properties are required; a concrete class can define methods and properties. It may also leave some optional
public class Car {
public string Make;
public void Drive() {
//Some code..
}
}
2.Abstract Class :
This class type can't be instantiated directly, Designed to be a base class for other classes, May include Abstract method(without implementation), Derived class must implement the abstract method.
public abstract class Shape {
public abstract double CalculateArea();
public void Display() => Console.WriteLine("Shape..");
}
3.Interface Class :
An interface defines a contract that implementing classes must follow. It specifies what methods or properties must be implemented but does not enforce how they should be implemented. In traditional interfaces, all members are abstract (no implementation). However, modern programming languages like C# allow interfaces to include default implementations for some members.
public interface IDriveable {
void Drive();
}
4.Static Class:
This type can't be instantiated, Contains only static members (methods, properties) which belong to the class itself rather than any object.
public static class MathUtilites {
public static double Add(double a, double b) => a + b;
}
5.Sealed Class :
This class can not be inherited by other classes. used to prevent further derivation.
public sealed class Singleton() {
public void ShowMessages {
//Some code..
}
}
6.Partial Class :
Split across multiple files. All parts are combined into a single class during compilation. Useful for large classes or auto-generated code.
//File 1
public partial class Customer {
public void printDetails() {
//Some code..
}
}
//File 2
public partial class Customer {
public void UpdateDetails() {
//Some code..
}
}
7. Generic Class :
This type defined with type parameters allowing code to be more reuseable and type safe.
public class Box<T> {
public T content {get; set;}
}
8.Nested Class :
Defined within a class, and it can access the private members of the outer class.
public class OuterClass {
private string secret = "Outer class secret";
public class NestedClass {
public void RevealSecret(OuterClass outer) {
Console.WriteLine(outer.secret);
}
}
}
9.Record Class :
Immutable reference type. primarily used to store data. Provides built-in functionality for equality checks and copying.
public record person(string Email, int age);
That's for today, Catch you up later.
Keep Learning Nerds!
Top comments (0)