Solution assumes your classes are topologically correct polygons, i.e. there are no gaps and overlaps between them.
Class defined by value stored in field GRIDCODE
#define boundary between polygons and delete outer ones
arcpy.PolygonToLine_management(in_features="polygons", out_feature_class="D:/Scratch/lines2D.shp", neighbor_option="IDENTIFY_NEIGHBORS")
arcpy.SelectLayerByAttribute_management(in_layer_or_view="lines2D", selection_type="NEW_SELECTION", where_clause=""""LEFT_FID" =-1""")
arcpy.DeleteFeatures_management(in_features="lines2D")
Create LEFT_CLASS and RIGHT_CLASS fields in lines table and populate them by relevant classes using joins to LEFT_FID and RIGHT_FID and polygons OID:
#transfer class on the left of the line to relevant line field
arcpy.AddJoin_management(in_layer_or_view="lines2D", in_field="LEFT_FID", join_table="polygons", join_field="FID", join_type="KEEP_ALL")
arcpy.CalculateField_management(in_table="lines2D", field="lines2D.LEFT_CODE", expression="[polygons.GRIDCODE]")
arcpy.RemoveJoin_management(in_layer_or_view="lines2D", join_name="")
#transfer class on the RIGHT of the line to relevant line field - repeat 3 processes above
# select lines that are boundaries between between 2 different classes
arcpy.SelectLayerByAttribute_management(in_layer_or_view="lines2D", selection_type="NEW_SELECTION", where_clause=""""LEFT_CODE" <> "RIGHT_CODE""""")
# find mean elevation
arcpy.InterpolateShape_3d(in_surface="dem", in_feature_class="lines2D", out_feature_class="D:/Scratch/lines_3D.shp", sample_distance="", z_factor="1", method="BILINEAR", vertices_only="DENSIFY", pyramid_level_resolution="0")