Logo

dev-resources.site

for different kinds of informations.

Mastering Generalization in OOP: Techniques and Examples

Published at
11/15/2024
Categories
oop
csharp
inheritance
interfaces
Author
moh_moh701
Categories
4 categories in total
oop
open
csharp
open
inheritance
open
interfaces
open
Author
10 person written this
moh_moh701
open
Mastering Generalization in OOP: Techniques and Examples

meta description: Learn key techniques for mastering generalization in object-oriented programming, including pull-up, push-down, extract interface, and superclass strategies, with clear examples and practical applications.

1. Pull Up Field

  • Scenario: Two child classes share the same attribute.
  • Solution: Move the attribute to the parent class to reduce redundancy.
  • Example: If Employee and Manager both have an Email attribute, move Email to the parent class Person:
  public class Person
  {
      public string Email { get; set; }
  }

  public class Employee : Person { ... }
  public class Manager : Person { ... }
Enter fullscreen mode Exit fullscreen mode

2. Pull Up Method

  • Scenario: Two child classes have the same method implementation.
  • Solution: Move the method to the parent class.
  • Example: If Rectangle and Circle both have a CalculateArea() method, move it to the parent class Shape:
  public abstract class Shape
  {
      public abstract double CalculateArea();
  }
Enter fullscreen mode Exit fullscreen mode

3. Push Down Field

  • Scenario: Only one child uses an attribute in the parent class.
  • Solution: Move the attribute to the child class to avoid unused fields or null values.
  • Example: If only Car uses the EngineType field in the parent class Vehicle, move EngineType to Car:
  public class Vehicle { ... }

  public class Car : Vehicle
  {
      public string EngineType { get; set; }
  }
Enter fullscreen mode Exit fullscreen mode

4. Push Down Method

  • Scenario: Only one child implements a specific method in the parent class.
  • Solution: Move the method to the child class to avoid throwing NotImplementedException.
  • Example: If only MobileApp implements the SendPushNotification() method in Software, move SendPushNotification() to MobileApp:
  public class Software { ... }

  public class MobileApp : Software
  {
      public void SendPushNotification() { ... }
  }
Enter fullscreen mode Exit fullscreen mode

5. Extract Interface

  • Scenario: Two or more classes have similar methods or properties.
  • Solution: Create an interface and move the shared declarations to it.
  • Example: For Printer and Scanner, create an IMachine interface:
  public interface IMachine
  {
      void Start();
      void Stop();
  }

  public class Printer : IMachine { ... }
  public class Scanner : IMachine { ... }
Enter fullscreen mode Exit fullscreen mode

6. Extract Subclass

  • Scenario: A class has features used only in specific cases.
  • Solution: Create a subclass and move these features to it.
  • Example: If Document has a GenerateDigitalSignature() method used only for LegalDocument, move the method to a LegalDocument subclass:
  public class Document { ... }

  public class LegalDocument : Document
  {
      public void GenerateDigitalSignature() { ... }
  }
Enter fullscreen mode Exit fullscreen mode

7. Extract Superclass

  • Scenario: Multiple classes share fields or methods.
  • Solution: Create a shared parent class and move the shared functionality to it.
  • Example: If Mobile, Tablet, and Laptop all have common fields (ModelName, Price, Brand) and methods, create a Gadget superclass:
  public class Gadget
  {
      public string ModelName { get; set; }
      public decimal Price { get; set; }
      public string Brand { get; set; }
  }

  public class Mobile : Gadget { ... }
  public class Tablet : Gadget { ... }
  public class Laptop : Gadget { ... }
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • These generalization techniques are effective for refactoring and improving code structure.
  • Updated examples ensure applicability across various domains, such as business, software, and devices.
  • Refactoring tools like those in Visual Studio can assist in implementing these techniques efficiently.

\

inheritance Article's
30 articles in total
Favicon
Code Smell 286 - Overlapping Methods
Favicon
Understanding Traits in PHP and How They Differ from Inheritance
Favicon
Understanding Classes and Inheritance in JavaScript
Favicon
Mastering Generalization in OOP: Techniques and Examples
Favicon
Upcasting — Using a Superclass Reference for a Subclass Object
Favicon
Mapping inheritance hierarchies with MapStruct
Favicon
Python Inheritance Explained: Types, Examples, and Best Practices
Favicon
Mastering TypeScript: Understanding the Power of extends
Favicon
PHP: Herança vs. Composição
Favicon
Method Resolution Order in Python 3
Favicon
Why we don't use RemoteWebDriver driver = new ChromeDriver()
Favicon
Understanding JavaScript Inheritance: A Deep Dive into Prototypal and Constructor Patterns
Favicon
Understanding Inheritance and Polymorphism: Simplified OOP Concepts
Favicon
JS Inheritance - Part 2: Factory Functions vs. Classes
Favicon
Inheritance with access-specifier in cpp
Favicon
Inheritance in Dart
Favicon
RoR - extend, < (inheritance), include - know the difference
Favicon
Inheritance vs composition: a fight against Egyptian gods
Favicon
Object Oriented Concept - Inheritance
Favicon
Exploring Component Inheritance in Aventus
Favicon
Python Inheritance
Favicon
Inheritance vs Composition: Using a Role-Playing Game in JavaScript as an Example
Favicon
PostgreSQL - Partitioning & Inheritance
Favicon
Prototype and Prototypical Inheritance
Favicon
Ruby - Inheritance
Favicon
MultiLevel Inheritance in Java
Favicon
Overloading vs Overriding in Typescript
Favicon
Java Error - at least one public class is required in main file
Favicon
Polymorphism in Java.
Favicon
Method Overriding in Java.

Featured ones: