DEV Community

Hamza
Hamza

Posted on

An Overview of the Main Class Types in OOP

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..
  }
}
Enter fullscreen mode Exit fullscreen mode

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..");
}
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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..
  }
}
Enter fullscreen mode Exit fullscreen mode

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..
  }
}
Enter fullscreen mode Exit fullscreen mode

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;}
}
Enter fullscreen mode Exit fullscreen mode

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);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

That's for today, Catch you up later.

Keep Learning Nerds!

Top comments (0)